Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In url an image is attached. I want to get row number when user click onto the button place into a gridview column. Code identify on which row user click on the button it will return the row number which contain that button....


https://onedrive.live.com/redir?resid=510F28FF6CA7EC19!108&authkey=!ANRpYLsB2bptcdo&v=3&ithint=photo%2cPNG[^]
Posted
Updated 17-Oct-15 2:40am
v2
Comments
Andy Lanng 17-Oct-15 8:39am    
don't shout - it's really, really rude!

Ok - the button should be a gridview template:

XML
<asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="Button" runat="server"
      CommandName="Button"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
      Text="Add to Cart" />
  </ItemTemplate>
</asp:TemplateField>


That way you have full control in code behind:
C#
protected void GridView1_RowCommand(object sender,
  GridViewCommandEventArgs e)
{
  if (e.CommandName == "Button")
  {
    // Retrieve the row index stored in the
    // CommandArgument property.
    int index = Convert.ToInt32(e.CommandArgument);

    // Retrieve the row that contains the button
    // from the Rows collection.
    GridViewRow row = GridView1.Rows[index];

  }

  }


Hope that helps ^_^
Andy
 
Share this answer
 
protected void gridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int index = e.Row.RowIndex;
}
}
 
Share this answer
 

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