c# - error creating dynamic controls -
i supposed create label
, textbox
when checkboxlist
event selectedindexchanged
fires.
i tried following code, giving me error:
multiple controls same id 'mylab1' found
protected void chkcar_selectedindexchanged(object sender, eventargs e) { // panel2.controls.clear(); string dat = datetime.parse(gridview1.selectedrow.cells[4].text).tostring("dd-mmm-yyyy"); lblname.text = ""; (int = 0; < chkcar.items.count; i++) { int sum = 0; if (chkcar.items[i].selected == true) { panel tt = new panel(); panel2.controls.add(new literalcontrol("<br />")); label mylab = new label(); mylab.id = "mylab" + nextid1; mylab.text = "no of persons in " + chkcar.items[i].value; panel2.controls.add(mylab); controlslist1.add(mylab.id); textbox mytext = new textbox(); mytext.id = "mytext" + nextid2; mytext.text = ""; panel2.controls.add(mytext); controlslist2.add(mytext.id); } } } protected override void loadviewstate(object savedstate) { base.loadviewstate(savedstate); foreach (string lbid in controlslist1) { label lb = new label(); lb.id = lbid; panel2.controls.add(lb); panel2.controls.add(new literalcontrol("<br>")); } foreach (string txtid in controlslist2) { textbox tb = new textbox(); tb.id = txtid; panel2.controls.add(tb); panel2.controls.add(new literalcontrol("<br>")); } } private list<string> controlslist1 //labels { { if (viewstate["controls"] == null) { viewstate["controls"] = new list<string>(); } return (list<string>)viewstate["controls"]; } } private list<string> controlslist2 //textboxes { { if (viewstate["controls"] == null) { viewstate["controls"] = new list<string>(); } return (list<string>)viewstate["controls"]; } } private int nextid1//label { { return controlslist1.count + 1; } } private int nextid2//textbox { { return controlslist2.count + 1; } }
can me knowing i'm going wrong? in advance.
i imagine make work need add newly added controls ids controlslist1
, controlslist2
, otherwise nextid1
, nextid2
return same number during for
loop.
if (chkcar.items[i].selected == true) { panel tt = new panel(); panel2.controls.add(new literalcontrol("<br />")); label mylab = new label(); mylab.id = "mylab" + nextid1; mylab.text = "no of persons in " + chkcar.items[i].value; panel2.controls.add(mylab); controlslist1.add(mylab.id) // <--- here textbox mytext = new textbox(); mytext.id = "mytext" + nextid2; mytext.text = ""; panel2.controls.add(mytext); controlslist2.add(mytext.id) // <--- , here }
Comments
Post a Comment