Click here to Skip to main content
12,818,785 members (29,549 online)
Rate this:
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 3-Feb-12 0:41am
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 3-Feb-12 12:55pm
Nice 5+
Anuja Pawar Indore 6-Feb-12 0:54am
Thanks Sridhar
direction.d 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 6-Feb-12 0:54am
Give ID to the ButtonField as ID="imgprint"
direction.d 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 7 Feb 2012
Copyright © CodeProject, 1999-2017
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