c# - Retrieving the selected checkbox values from a ValidationGroup -
i have page dynamically populates listview checkboxes based on database. text checkboxes equal usernames of users in database. trying create control in usernames in selected checkboxes deleted. this, plan on using foreach loop run each selected checkbox within validationgroup.
first, here asp .net code, displaying approach formatting page.
<asp:listview id="lvusers" runat="server"> <itemtemplate> <asp:checkbox id="chkuser" runat="server" text='<%# eval("username") %>' validationgroup="usercheck" /><br /> </itemtemplate> </asp:listview>
here current (broken) code, attempting run foreach loop each listview item, when should running foreach loop each selected checkbox.
foreach (listviewitem item in lvusers.items) //trying replace "for each selected checkbox within usercheck validationgroup". { int userid = 0; string sqlstatement = "select userid users username = " + item; //this should selecting username = text value of each selected checkbox. sqlconnection conn = new sqlconnection(webconfigurationmanager.connectionstrings["connectionstring"].connectionstring); sqlcommand comm = new sqlcommand(sqlstatement, conn); conn.open(); sqldatareader reader = comm.executereader(); while (reader.read()) { userid = (int)reader["userid"]; } reader.close(); conn.close(); //down here delete connected values various tables based on value obtained userid above. }
any on appreciated.
using nice controlfinder class given jimmy in better way find control in asp.net, can retrieve checkboxes recursively in listview , test validationgroup
:
controlfinder<checkbox> finder = new controlfinder<checkbox>(); finder.findchildcontrolsrecursive(lvusers); foreach (checkbox chkbox in finder.foundcontrols) { if (chkbox.checked && chkbox.validationgroup == "usercheck") { // } }
Comments
Post a Comment