Add checkbox column in asp.net gridview

I have a few questions when it comes to adding a CheckBox column to a gridview in asp.net and getting multiple values. Firstly, I see that everyone added OnCheckedChanged="chkview_CheckedChanged" to their aspx page, but when you click on CheckBox to set its actions, it does not open OnCheckedChanged="chkview_CheckedChanged" . Instead, it fires the SelectedIndexChanged event. I try to do when they select CheckBox , it adds information about the corresponding lines to the TextBox . Here is what I am using to set the values. How can I use the selected CheckBox instead?

 protected void dropGridView_SelectedIndexChanged1(object sender, EventArgs e) { GridViewRow row = dropdeadGridView.SelectedRow; IDTextBox.Text = row.Cells[1].Text; loadnumTextBox.Text = row.Cells[2].Text; } 

Once you are done with this, how can you do it where it will receive each checked line, and not just one, which is my current problem. I am looking for a way to select multiple rows and select a select button. I searched a lot and can't find anything, so I'm trying to accomplish this with CheckBoxes . Any ideas how I can add this and get a few lines that I can select. Thank you in advance.

Here is my edit * Posting ASP code for a CheckBox column:

 <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="SelectCheckBox" runat="server" OnCheckedChanged="SelectCheckBox_OnCheckedChanged"/> </ItemTemplate> </asp:TemplateField> 
+6
source share
1 answer

First you must set the autopostback attribute to true:

 <asp:CheckBox ID="SelectCheckBox" runat="server" AutoPostBack="true" OnCheckedChanged="SelectCheckBox_OnCheckedChanged"/> 

In your case, SelectedIndexChanged dispatched using gridview. For the flag event, you should use the OnCheckedChanged event:

 protected void SelectCheckBox_OnCheckedChanged(object sender, EventArgs e) { CheckBox chk = sender as CheckBox ; if(chk.Checked) { GridViewRow row = (GridViewRow)chk.NamingContainer; IDTextBox.Text = row.Cells[1].Text; loadnumTextBox.Text = row.Cells[2].Text; } } 

If you want to iterate over all selected checkboxes:

 var rows = dropdeadGridView.Rows; int count = dropdeadGridView.Rows.Count; for (int i = 0; i < count; i++) { bool isChecked = ((CheckBox)rows[i].FindControl("chkBox")).Checked; if(isChecked) { //Do what you want } } 
+9
source

All Articles