asp.net - How can itereate through checkboxes using forlopp in C#? -
i need check whether checkbox values ticked or not using 'for' loop.
checkbox chkbox = (checkbox)tab_patients.controls[chk + i]; from searches got line. tab_patients denote ?`
string chk = "checkbox"; (int = 1; < 13; i++) { (int k = 1; k < 4; k++) { // checkbox chkbox = (checkbox)tab_patients.controls[chk + i]; checkbox cb = (checkbox)this.page.form.findcontrol("chk"+i.tostring()); if(cb.checked==true) { check = + "0" + k; } }}
make life easier , create checkboxes checkboxlists: http://msdn.microsoft.com/de-de/library/system.web.ui.webcontrols.checkboxlist.aspx
<asp:checkboxlist id="someelement" runat="server" autopostback="true" repeatcolumns="4" enableviewstate="true"> </asp:checkboxlist> with repeatcolumns property telling element, infact want 4 columns, control devide 32 elements 4 , group checkboxes pack of 8.
after this, can iterate through every list this:
foreach(var cb in checkboxlistid) { if(cb.checked) { //do } } on top of that, here old code showing how fill checkboxlist data fetched sql-query. can ignore if expression, checking if there cached data, fill checkboxes.
using (oledbconnection connection = new oledbconnection(configurationmanager.connectionstrings["database"].connectionstring)) { string query = @"select * level"; oledbcommand command = new oledbcommand(query, connection); if (session["savedlevelitems"] != null) { checkboxlistlevel.items.clear(); list<listitem> sessionlist = (list<listitem>)session["savedlevelitems"]; foreach (var item in sessionlist) { try { checkboxlistlevel.items.add(item); } catch { } } } else { connection.open(); checkboxlistlevel.datatextfield = "bez_level"; checkboxlistlevel.datavaluefield = "id_level"; oledbdatareader listreader = command.executereader(); checkboxlistlevel.datasource = listreader; checkboxlistlevel.databind(); listreader.close(); listreader.dispose(); } }
Comments
Post a Comment