Click here to Skip to main content
11,637,002 members (68,118 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET

I added a ButtonField in the Gridview like..

<asp:GridView ID="GridView1" runat="server">
        <asp:ButtonField ButtonType="Image" ImageUrl="ImagePath" />

Now How can i Change the URL of the ButtonField at runtime in OnClick Event??
Posted 2-Feb-12 23:41pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

<asp:ButtonField ButtonType="Image" ImageUrl="~/Images/pdf.png"  CommandName="pdf_click" ID="imgprint"/>
Code behind
void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
 // If multiple buttons are used in a GridView control, use the
 // CommandName property to determine which button was clicked.
   // Convert the row index stored in the CommandArgument
   // property to an Integer.
   int index = Convert.ToInt32(e.CommandArgument);
   // Retrieve the row that contains the button clicked
   // by the user from the Rows collection.
  ButtonField cf = (ButtonField)e.Row.FindControl("imgprint");
  cf.ImageUrl = "images/something.png";
   // Now you have access to the gridviewrow.
Sridhar Patnayak at 3-Feb-12 12:55pm
Nice 5+
Anuja Pawar Indore at 6-Feb-12 0:54am
Thanks Sridhar
direction.d at 4-Feb-12 0:01am
I tried your code but in the ButtonField property ID (ID="imgprint") is Not available. So it gives me an error like..

Type 'System.Web.UI.WebControls.ButtonField' does not have a public property named 'ID'

And in RowCommand event Row method is not available..
ButtonField cf = (ButtonField)e.Row.FindControl("imgprint");

so it gives a below error..
'System.Web.UI.WebControls.GridViewCommandEventArgs' does not contain a definition for 'Row' and no extension method 'Row' accepting a first argument of type 'System.Web.UI.WebControls.

And Yes i used a ButtonField in Column(one ButtonField for each record) in the Gridview...

Any solutions for these errors??
Anuja Pawar Indore at 6-Feb-12 0:54am
Give ID to the ButtonField as ID="imgprint"
direction.d at 7-Feb-12 1:26am
Yes i gave it...(ID Property is not availabelfor the columns of the gridview)
Then it gives me an error like above..????

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 stibee 313
1 295
2 OriginalGriff 263
3 Sergey Alexandrovich Kryukov 240
4 Suvendu Shekhar Giri 225
0 OriginalGriff 8,941
1 Sergey Alexandrovich Kryukov 8,493
2 Mika Wendelius 6,909
3 F-ES Sitecore 2,388
4 Suvendu Shekhar Giri 2,320

Advertise | Privacy | Mobile
Web03 | 2.8.150728.1 | Last Updated 7 Feb 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100