Monday 15 February 2010

asp.net - create dynamic controls on button click -



asp.net - create dynamic controls on button click -

i trying create dynamic controls on button click using next code. creates textbox dynamically first time not create 2nd, 3rd or 4th etc controls. issue?

<asp:placeholder id="placeholder1" runat="server"></asp:placeholder> <asp:button id="addtext" runat="server" text="add" onclick="addnewtext_click" /> protected int numberofcontrols { get{return (int)viewstate["numcontrols"];} set{viewstate["numcontrols"] = value;} } private void page_load(object sender, system.eventargs e) { if(!page.ispostback) this.numberofcontrols = 0; } protected void addnewtext_click(object sender, eventargs e) { textbox tx = new textbox(); tx.id = "controlid_" + numberofcontrols.tostring(); placeholder1.controls.add(tx); this.numberofcontrols++; }

when create this, view state shouldn't updated. each time nail server add together 1 command , others missing. since place holder @ specific location in view state adding controls can cause problems. when create dynamic controls , when other static controls created @ different times. when view state loaded, dynamic controls don't exist yet, view state has info them. @ below link.

check link out

asp.net

No comments:

Post a Comment