Asp.net user control delete dynamically created control
<p>I have created a UserControl that has a Button and a Panel (TxtDiv), During onclick of the button, textboxes are added to the existing textboxes in TxtDiv, by default 2 textboxes are shown.</p>

<p>Now I have created buttons near all textboxes which should have the functionality to delete the respective textbox and the button itself should also be removed, I have created this method but the problem is when I delete another textbox a new textbox is generate in place of previously deleted textbox.</p>

<p>The method for deletion is as follows:</p>

<pre><code>void Del_Click(object sender, EventArgs e)
{
    Button bt = sender as Button;
    string abc = bt.ID;
    string txtID=abc.Substring(4);
    string deltxt = "txt" + txtID;
    //int noOfCtrl=TxtDiv.Controls.Count;
    foreach (Control c in TxtDiv.Controls)
    {
        if (c.ID == deltxt) {
            TxtDiv.Controls.Remove(c);
            TxtDiv.Controls.Remove(bt);
            break;
        }
    }
}
</code></pre>

<p>This is my add and create method:-</p>

<p><strong>Onload Method</strong></p>

<pre><code>  protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
        int num = 0;
        num = Convert.ToInt32(sizeBox);
        for (int i = 0; i &lt; num; i++)
        {
            Button bttn = new Button();
            TextBox txt = new TextBox();
            txt.ID = "txt" + i.ToString();
            bttn.ID = "bttn" + i.ToString();
            bttn.Text = "Delete";
            bttn.Click += new EventHandler(Del_Click);
            TxtDiv.Controls.Add(txt);
            TxtDiv.Controls.Add(bttn);
            TxtDiv.Controls.Add(new LiteralControl("&lt;br /&gt;"));

        }
        for (int i = 0; i &lt; TotalNumberAdded; ++i)
        {
            AddControls(i + 1);
        //}
        }

    }
</code></pre>

<p><strong>For Adding control:-</strong></p>

<pre><code>private void AddControls(int controlNumber)
{
    var newbutton = new Button();
    var newTextbox = new TextBox();
    // textbox needs a unique id to maintain state information
    newTextbox.ID = "txt_" + controlNumber;
    newbutton.ID = "bttn_" + controlNumber.ToString();
    //newTextbox.Text = "";
    // add the label and textbox to the panel, then add the panel to the form
    newbutton.Text = "Delete";
    newbutton.Click += new EventHandler(Del_Click);
    TxtDiv.Controls.Add(newTextbox);
    TxtDiv.Controls.Add(newbutton);
    TxtDiv.Controls.Add(new LiteralControl("&lt;br /&gt;"));
    contDiv.Controls.Add(TxtDiv);
    //TxtDiv.Controls.Add(newPanel);
}

protected int TotalNumberAdded
{
    get { return (int)(ViewState["TotalNumberAdded"] ?? 0); }
    set { ViewState["TotalNumberAdded"] = value; }
}
</code></pre>

<p>If there is a better and appropriate way to delete without using <code>id</code> then please help me to get through it, thanks.</p>
