c# - How to add CheckBox, Label and DDL to ASP.NET page programmatically? -
i trying add check box, label , ddl asp.net page (aspx) class in c#. have been using literalcontrol _litext = new literalcontrol();
attach label can show them using this.controls.add(_litext);
in createchildcontrols() method.
how add ddl , check box asp.net page c# code label in same line ddl , checkbox?
i have made ddl using syntax:
list<dropdownlist> _ddlcollection=new list<dropdownlist>(); (int = 0; < 5; i++) { _ddlcollection.add(new dropdownlist()); }
problem not in this.controls.add() call createchildcontrols(). onprerender() method fill ddl , check box. literalcontrol class this? here have tried in onprerender():
foreach (splist list in web.lists) { if (!list.hidden) { _litext.text += @<input type="checkbox">; _litext.text += list.title + "<br />"; } }
first, should add placeholder.
<form id="form1" runat="server"> <asp:placeholder runat="server" id="phmain"></asp:placeholder> </form>
next, if have add in 1 line use table (it simple not recommended method, next step add page , set css style page).
protected override void createchildcontrols() { base.createchildcontrols(); table table = new table(); (int = 0; < 3; i++) { tablerow tr = new tablerow(); tablecell tc1 = new tablecell(); tc1.controls.add(new literalcontrol(string.format("line {0}",i))); tr.cells.add(tc1); tablecell tc2 = new tablecell(); checkbox chb = new checkbox(); chb.id = string.format("checkbox_{0}", i); chb.text = string.format("checkbox {0}", i); chb.checkedchanged += chb_checkedchanged; chb.autopostback = true; tc2.controls.add(chb); tr.cells.add(tc2); tablecell tc3 = new tablecell(); dropdownlist ddl = new dropdownlist(); ddl.id = string.format("dropdownlist_{0}", i); ddl.items.add("1111"); ddl.items.add("2222"); ddl.items.add("3333"); ddl.selectedindex = i; ddl.enabled = false; tc3.controls.add(ddl); tr.cells.add(tc3); table.rows.add(tr); } phmain.controls.add(table); } void chb_checkedchanged(object sender, eventargs e) { checkbox chb = sender checkbox; string ddlid = chb.id.replace("checkbox", "dropdownlist"); dropdownlist ddl = this.page.findcontrol(ddlid) dropdownlist; if (ddl != null) { ddl.enabled = chb.checked; } }