Finding other controls in same gridview row from the event method of a control


/ Published in: C#
Save to your folder(s)

Sometimes we need to have checkboxes in the GridView control and we want that as soon as the Checkbox is clicked/checked the postback happens and we get the value from the first row in the GridView control.

This can be easily done by using the following code:


Copy this code and paste it in your HTML
  1. <asp:GridView ID="GridView1" runat="server"
  2. AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">
  3. <Columns>
  4. <asp:BoundField DataField="CategoryID"
  5. HeaderText="CategoryID" />
  6. <asp:BoundField DataField="CategoryName"
  7. HeaderText="CategoryName" />
  8. <asp:TemplateField HeaderText="Select">
  9. <ItemTemplate>
  10. <asp:CheckBox ID="CheckBox1"
  11. AutoPostBack="true" runat="server"
  12. OnCheckedChanged="CheckBox1_CheckedChanged1" />
  13.  
  14. </ItemTemplate>
  15. </asp:TemplateField>
  16. </Columns>
  17. </asp:GridView>
  18.  
  19. And the code behind:
  20.  
  21. protected void CheckBox1_CheckedChanged1(object sender, EventArgs e)
  22. {
  23. CheckBox checkbox = (CheckBox)sender;
  24. GridViewRow row = (GridViewRow) checkbox.NamingContainer;
  25. Response.Write(row.Cells[0].Text);
  26. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.