Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Hi,
 
I added a ButtonField in the Gridview like..
 
<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 2-Feb-12 23:41pm
Rate this: bad
good
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.
 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.
  }
}
  Permalink  
v2
Comments
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
   
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 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 Sergey Alexandrovich Kryukov 339
1 Nirav Prabtani 248
2 OriginalGriff 234
3 _Amy 165
4 Maciej Los 145
0 OriginalGriff 7,744
1 Sergey Alexandrovich Kryukov 6,956
2 Maciej Los 3,994
3 Peter Leow 3,708
4 CHill60 2,742


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 7 Feb 2012
Copyright © CodeProject, 1999-2014
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