Adding code is working proper but how to delete ?
%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["AddControl"] = 1;
}
}
protected void add_Click(object sender, EventArgs e)
{
add_button(Convert.ToInt32(ViewState["AddControl"]));
}
protected void add_button(int i)
{
for (int j = 0; j < i; j++)
{
TextBox textbox_foradd = new TextBox();
textbox_foradd.ID = "textadd" + (j + 1).ToString();
PlaceHolder1.Controls.Add(textbox_foradd);
Button btn = new Button();
btn.Text = "X";
textbox_foradd.ID = "btn" + (j + 1).ToString();
PlaceHolder1.Controls.Add(btn);
}
ViewState["AddControl"] = i + 1;
}
{
if (!IsPostBack)
{
ViewState["AddControl"] = 1;
}
}
protected void add_Click(object sender, EventArgs e)
{
add_button(Convert.ToInt32(ViewState["AddControl"]));
}
protected void add_button(int i)
{
for (int j = 0; j < i; j++)
{
TextBox textbox_foradd = new TextBox();
textbox_foradd.ID = "textadd" + (j + 1).ToString();
PlaceHolder1.Controls.Add(textbox_foradd);
Button btn = new Button();
btn.Text = "X";
textbox_foradd.ID = "btn" + (j + 1).ToString();
PlaceHolder1.Controls.Add(btn);
}
ViewState["AddControl"] = i + 1;
}

VulpesPosted Mar 25, 2014, 10:32 AM
Clicking any of the dynamic buttons themselves will likewise remove them all.
If you want to start from scratch next time you click the 'Add TextBox' button, you can reset the AddControl variable to 1 in the Click handler:
If you don't do that, then it will of course use the previous value stored in ViewState.
Abhay ShankerPosted Mar 25, 2014, 8:53 AM
http://msdn.microsoft.com/en-us/library/82785s1h(v=vs.90).aspx