ImageButton Column in DataGrid

R

Ron

I'm trying to add an ImageButton Column into my datagrid,
but the Property Designer only allows for plain buttons
and link buttons. Is there a way to use image buttons,so
this button matches the rest of the theme of my site?

Thanks!
 
R

Ron

How do I find the ItemDataBound event? Forgive me, I'm new
to this and didn't write the code.

I understand what you're saying, but I'm not sure how to
implement it. The other thing I'm unclear about is you
mention the cell with the button. There could be an
unlimited number of cells with buttons, since every cell
in the column will have a button.
-----Original Message-----
Try following:

1. In ItemDataBound event set Attributes
["class='buttonStyle'] for the cell
 
E

Eliyahu Goldin

Try following:

1. In ItemDataBound event set Attributes["class='buttonStyle'] for the cell
with button.
2. Attach a stylesheet with a style '.buttonStyle' and set in '.buttonStyle'
background-image attribute to the image you want.

Eliyahu
 
K

Ken Cox [Microsoft MVP]

Hi Ron,

You could change the column to a template and then insert the image button
in place of the regular button. In the following, the first is a plain
button, the second uses a template with an image button.

<asp:ButtonColumn Text="Select" ButtonType="PushButton"
HeaderText="Plain button" CommandName="Select"></asp:ButtonColumn>

<asp:TemplateColumn HeaderText="Image button">
<ItemTemplate>
<asp:ImageButton id="ImageButton1" runat="server"
ImageUrl="http://www.gc.ca/images/canada.gif"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateColumn>

Does this help?

Ken
Microsoft MVP [ASP.NET]
 
E

Eliyahu Goldin

ItemDataBound event is one of the events supported by the DataGrid class. It
is fired once for every row, including the header and the footer if any. The
event handler gets a parameter representing a datagrid row. It has
collection of cells, one of them is your column with buttons.

Eliyahu

Ron said:
How do I find the ItemDataBound event? Forgive me, I'm new
to this and didn't write the code.

I understand what you're saying, but I'm not sure how to
implement it. The other thing I'm unclear about is you
mention the cell with the button. There could be an
unlimited number of cells with buttons, since every cell
in the column will have a button.
-----Original Message-----
Try following:

1. In ItemDataBound event set Attributes
["class='buttonStyle'] for the cell
with button.
2. Attach a stylesheet with a style '.buttonStyle' and set in '.buttonStyle'
background-image attribute to the image you want.

Eliyahu




.
 
R

Ron

Thanks. That worked. I had a feeling it had something to
do with TemplateColumns. Inorder to get it to process the
command I want, just add the CommandName like such?

<asp:ImageButton id="ImageButton1" runat="server"
ImageUrl="../Graphics/help.gif"
-----Original Message-----
Hi Ron,

You could change the column to a template and then insert the image button
in place of the regular button. In the following, the first is a plain
button, the second uses a template with an image button.

<asp:ButtonColumn Text="Select" ButtonType="PushButton"
HeaderText="Plain button"
CommandName="Select"> said:
<asp:TemplateColumn HeaderText="Image button">
<ItemTemplate>
<asp:ImageButton id="ImageButton1" runat="server"
ImageUrl="http://www.gc.ca/images/canada.gif"></asp:ImageB utton>
</ItemTemplate>
</asp:TemplateColumn>

Does this help?

Ken
Microsoft MVP [ASP.NET]

Ron said:
I'm trying to add an ImageButton Column into my datagrid,
but the Property Designer only allows for plain buttons
and link buttons. Is there a way to use image buttons,so
this button matches the rest of the theme of my site?

Thanks!

.
 
K

Ken Cox [Microsoft MVP]

Hi Ron,

You've got it. In the ItemCommand event, just check the CommandName to be
sure that's the button that was clicked:

Private Sub DataGrid1_ItemCommand _
(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls. _
DataGridCommandEventArgs) _
Handles DataGrid1.ItemCommand
If e.CommandName = "Cmdimgbutton" Then
Response.Write("Clicked " & _
e.CommandName & " on row " & _
e.Item.ItemIndex.ToString)
End If
End Sub

You're on your way!

Ken
MVP [ASP.NET]

Ron said:
Thanks. That worked. I had a feeling it had something to
do with TemplateColumns. Inorder to get it to process the
command I want, just add the CommandName like such?

<asp:ImageButton id="ImageButton1" runat="server"
ImageUrl="../Graphics/help.gif"
-----Original Message-----
Hi Ron,

You could change the column to a template and then insert the image button
in place of the regular button. In the following, the first is a plain
button, the second uses a template with an image button.

<asp:ButtonColumn Text="Select" ButtonType="PushButton"
HeaderText="Plain button"
CommandName="Select"> said:
<asp:TemplateColumn HeaderText="Image button">
<ItemTemplate>
<asp:ImageButton id="ImageButton1" runat="server"
ImageUrl="http://www.gc.ca/images/canada.gif"></asp:ImageB utton>
</ItemTemplate>
</asp:TemplateColumn>

Does this help?

Ken
Microsoft MVP [ASP.NET]

Ron said:
I'm trying to add an ImageButton Column into my datagrid,
but the Property Designer only allows for plain buttons
and link buttons. Is there a way to use image buttons,so
this button matches the rest of the theme of my site?

Thanks!

.
 
R

Ron

Worked. Just wanted to say thanks again!
-----Original Message-----
Hi Ron,

You've got it. In the ItemCommand event, just check the CommandName to be
sure that's the button that was clicked:

Private Sub DataGrid1_ItemCommand _
(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls. _
DataGridCommandEventArgs) _
Handles DataGrid1.ItemCommand
If e.CommandName = "Cmdimgbutton" Then
Response.Write("Clicked " & _
e.CommandName & " on row " & _
e.Item.ItemIndex.ToString)
End If
End Sub

You're on your way!

Ken
MVP [ASP.NET]

Thanks. That worked. I had a feeling it had something to
do with TemplateColumns. Inorder to get it to process the
command I want, just add the CommandName like such?

<asp:ImageButton id="ImageButton1" runat="server"
ImageUrl="../Graphics/help.gif"
-----Original Message-----
Hi Ron,

You could change the column to a template and then
insert
the image button
in place of the regular button. In the following, the first is a plain
button, the second uses a template with an image button.

<asp:ButtonColumn Text="Select" ButtonType="PushButton"
HeaderText="Plain button"
CommandName="Select"> said:
<asp:TemplateColumn HeaderText="Image button">
<ItemTemplate>
<asp:ImageButton id="ImageButton1" runat="server"
ImageUrl="http://www.gc.ca/images/canada.gif"></asp:Imag
eB
utton>
</ItemTemplate>
</asp:TemplateColumn>

Does this help?

Ken
Microsoft MVP [ASP.NET]

I'm trying to add an ImageButton Column into my datagrid,
but the Property Designer only allows for plain buttons
and link buttons. Is there a way to use image buttons,so
this button matches the rest of the theme of my site?

Thanks!

.

.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top