c# - Checkbox is only getting value from one checkbox. I need to get value for multiple checkboxes when selected -
i'm having issues trying checkbox receive multiple values when checked. i'm using dynamic gridview. when checkbox checked , start button clicked services suppose start. 1 service starting , value other services checked reading false. know i'm doing wrong. in advance!
<asp:panel id="panel1" runat="server"> <asp:gridview id="gridview1" runat="server" style="font-size: 15pt" autogeneratecolumns="false"> <columns> <asp:boundfield datafield="servicename" headertext="service name" /> <asp:boundfield datafield="description" headertext="description" /> <asp:boundfield datafield="status" headertext="status" /> <asp:templatefield> <itemtemplate> <asp:checkbox id="chkstatus" enabled="true" runat="server" /> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> </asp:panel> protected void strbtn_click(object sender, eventargs e) { var dt = new datatable(); dt.columns.add("servicename", typeof(string)); dt.columns.add("description", typeof(string)); dt.columns.add("status", typeof(string)); string include = txtbox_inclusion.text; string exclude = txtbox_exclusion.text; servicecontroller[] services = servicecontroller.getservices(); foreach (servicecontroller service in services) { if (service.servicename.startswith(include) && !service.servicename.contains(exclude)) { var dro = dt.newrow(); dro["servicename"] = service.servicename; dro["description"] = service.displayname; dro["status"] = service.status; dt.rows.add(dro); (int = 0; < gridview1.rows.count; i++)//loop gridview rows { checkbox cb = (checkbox)gridview1.rows[i].cells[0].findcontrol("chkstatus"); //find checkbox if (cb != null) { if (cb.checked) { service.start(); service.waitforstatus(servicecontrollerstatus.running); dro["status"] = service.status; } else if (cb.checked && service.status == servicecontrollerstatus.running) { cb.enabled = false; } } } gridview1.datasource = dt; gridview1.databind(); } } }
using loop iterate through collection of objects isn't right tool job. consider implementing linq's foreach
loop ensure you're going through every sing object.
for example, instead of using for (int = 0; < gridview1.rows.count; i++)
, replace with: foreach(gridviewrow row in gridview1.rows)
.
Comments
Post a Comment