c# - Event on dynamically created checkbox asp.net -
i started programming asp.net, have table checkboxes.
the problem is, can't create static tables, because action linked parameters. anyway.. when click on first checkbox want invert other checkboxes in table. how can catch event?
<%@ page title="fragebogen generieren" language="c#" masterpagefile="~/site.master" autoeventwireup="true" codebehind="generate.aspx.cs" inherits="maxxrec.generate" smartnavigation="true" %> <asp:content id="bodycontent" contentplaceholderid="maincontent" runat="server"> <h2><%: title %>.</h2><br /> <br /> <asp:panel id="pcustomize" runat="server"></asp:panel> <br /><br /> <asp:button id="btnsave" class="btn btn-default" text="save" runat="server" onclick="btnsave_click"></asp:button> </asp:content> private bool selectthedata() { dtquestionblock = taquestionblock.getdata(); try { int rows = dtquestionblock.rows.count; (int = 0; < rows; i++) { updatepanel updatepanel = new updatepanel(); updatepanel.id = "up" + dtquestionblock.rows[i][1].tostring(); label lbl = new label(); lbl.id = "lbl" + dtquestionblock.rows[i][1].tostring(); lbl.cssclass = "h4"; lbl.attributes.add("runat", "server"); lbl.text = dtquestionblock.rows[i][1].tostring(); pcustomize.controls.add(lbl); pcustomize.controls.add(new literal() { id = "br" + i, text = "<br /><br />" }); htmltable tbl = new htmltable(); tbl.width = "100%"; tbl.attributes.add("class", "table"); tbl.id = "htmltbl" + dtquestionblock.rows[i][1].tostring(); htmltablerow htr = new htmltablerow(); htmltablecell hcella = new htmltablecell(); checkbox acb = new checkbox(); acb.id = "cb" + dtquestionblock.rows[i]["name"].tostring(); //acb.checkedchanged += new eventhandler(cb_checkedchanged); hcella.width = "30px"; hcella.controls.add(acb); htr.cells.add(hcella); htmltablecell hcellf = new htmltablecell(); hcellf.innertext = "frage"; hcellf.style.add("font-weight", "bold"); hcellf.style.add("font-size", "15px"); htr.cells.add(hcellf); tbl.rows.add(htr); string cont = dtquestionblock.rows[i]["id"].tostring(); dtquestion = taquestion.getdataby1(convert.toint32(cont)); ncounttables = i; (int j = 0; j < dtquestion.rows.count; j++) { htmltablerow tr = new htmltablerow(); htmltablecell cell = new htmltablecell(); acb = new checkbox(); acb.id = "cb" + dtquestion.rows[j]["content"].tostring(); cell.width = "30px"; cell.controls.add(acb); tr.cells.add(cell); cell = new htmltablecell(); cell.innertext = dtquestion.rows[j]["content"].tostring(); cell.id = "cell" + j + "_" + dtquestion.rows[j]["content"].tostring(); tr.cells.add(cell); tbl.rows.add(tr); } updatepanel.contenttemplatecontainer.controls.add(tbl); //tbl.visible = false; pcustomize.controls.add(updatepanel); pcustomize.controls.add(new literal() { id = "br" + + rows, text = "<br />" }); } return true; } catch (exception ex) { type cstype = ex.gettype(); clientscriptmanager cs = page.clientscript; string cstext = ex.tostring(); cs.registerstartupscript(cstype, "popupscript", cstext, true); return false; } { taquestionblock.dispose(); dtquestionblock.dispose(); } }
you can try code
list<checkbox> lstchckbox; protected void page_load(object sender, eventargs e) { // can create controls programaticaly or html page, doesnt important //only should know controls id , controls share same checked event checkbox chc1 = new checkbox(); chc1.checkedchanged += new eventhandler(chck_checkedchanged); checkbox chc2 = new checkbox(); chc2.checkedchanged += new eventhandler(chck_checkedchanged); checkbox chc3 = new checkbox(); chc3.checkedchanged += new eventhandler(chck_checkedchanged); // now, can create list event fired, can catch controls checked or not lstchckbox = new list<checkbox>(); lstchckbox.add(chc1); lstchckbox.add(chc2); lstchckbox.add(chc3); } void chck_checkedchanged(object sender, eventargs e) { checkbox checkbox = (sender checkbox); foreach (checkbox item in lstchckbox) { if (item != checkbox) { item.checkedchanged -= new eventhandler(chck_checkedchanged); item.checked = !checkbox.checked; item.checkedchanged += new eventhandler(chck_checkedchanged); } } }
Comments
Post a Comment