Get id of selected checkboxes in gridview (Asp.net) C #

I have two columns for id and others for checkboxes. I took the checkboxes inside gridview. I wanted to see the checked values ​​inside the gridview, if the checkboxes are checked, then I want these values ​​ie id Asp.net

+4
source share
4 answers
foreach(Gridviewrow gvr in Gridview1.Rows) { if(((CheckBox)gvr.findcontrol("CheckBox1")).Checked == true) { int uPrimaryid= gvr.cells["uPrimaryID"]; } } 
+4
source

What you need to do is use the template field:

 <asp:TemplateField HeaderText="Field"> <ItemTemplate> <div style="display: block"> <asp:Checkbox Checked='<%# DataBinder.Eval(Container.DataItem,"Field") %>' runat="server" ID="chkField"></asp:Label> </div> </ItemTemplate> </asp:TemplateField> 

Then you can do:

 foreach (DataGridRow dr in DataGrid1.Rows) { ((CheckBox)gvr.FindControl("chkField")).Checked } 

to check if it is checked

+1
source

in your aspx you have the following

 <asp:GridView ID="gridViewID" runat="server" DataKeyNames="DataKey1,DataKey2,DataKey3" > <Columns> <asp:TemplateField HeaderText="selected"> <ItemTemplate> <asp:CheckBox ID="checkBoxID" runat="server" Checked='<%# Bind("Selected") %>' OnCheckedChanged="checkBoxID_CheckedChanged" AccessKey='<%# Container.DataItemIndex %>' AutoPostBack="True" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> 

Then in your event handler you do something similar to this:

 protected void checkBoxID_CheckedChanged(object sender, EventArgs e) { var checkbox = (CheckBox)sender; var rowIndex = Convert.ToInt32(checkbox.AccessKey); var gridView = GetErhebungModulGridView(); var dataKey = gridView.DataKeys[rowIndex]; if (dataKey != null) { var dataKey1 = dataKey["DataKey1"]; var dataKey2 = dataKey["DataKey2"]; var dataKey3 = dataKey["DataKey3"]; //Do something with the variables keys above } } 
+1
source
 <asp:gridview id="gv" runat="server"> <columns> <asP:TemplateField> <Asp:checkbox id="chk" runat="server" /> <Asp:literal id="ltlID" runat="server" visible="false" text='<%#eval("ID")%>' /> </asp:TemplateField> </columns> </asp:gridview> For each row as gridviewrow in gv.rows if ctype(row.findcontrol("chk"),checkbox).checked then Dim _ID as integer = ctype(row.findcontrol("ltlID"),literal).text end if next 
0
source

Source: https://habr.com/ru/post/1346046/


All Articles