Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I added a ButtonField in the Gridview like..

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



Now How can i Change the URL of the ButtonField at runtime in OnClick Event??
Posted

C#
<asp:ButtonField ButtonType="Image" ImageUrl="~/Images/pdf.png"  CommandName="pdf_click" ID="imgprint"/>

Code behind
C#
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.
 if(e.CommandName=="pdf_click")
  {
   // 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.
  }
}
 
Share this answer
 
v2
Comments
Sridhar Patnayak 3-Feb-12 12:55pm    
Nice 5+
Anuja Pawar Indore 6-Feb-12 0:54am    
Thanks Sridhar
disha desani 4-Feb-12 0:01am    
Hi,
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"
disha desani 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900