Posted By

leonbda on 05/29/09


Tagged

checkbox sender GridView NamingContainer


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


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


 / Published in: C#
 

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:

  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  

You need to login to post a comment.