I am trying to create dynamic controls on button click using the following code. It creates the Textbox dynamically first time but it does not create the 2nd, 3rd or 4th etc controls. What could be the 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++;
}