Remove dynamic textboxes RRS feed

  • Question

  • User-1651858287 posted

    I have some dynamic text boxes, the contains are saved to a database.

    After they are saved. I want to destroy them.

    This is what I have so far.

              // _arrViewState is the number of textboxes 
              // pnDuies a panel where the textbox are created
               for(int i = 1; i <=_arrViewState; i++) 
                    pnDuties.Controls.Remove(pnDuties.FindControl("Textbox_" + i + "_"));


    Thursday, March 14, 2019 12:32 PM

All replies

  • User-2054057000 posted

    How are you adding dynamic textboxes?

    What you have to do is stop that code execution on the click of the button.

    Thursday, March 14, 2019 3:54 PM
  • User-1174608757 posted

    Hi sweetSteal,

    According to your description, I have made a sample here.You could just package the function to  create  textbox then you could delete the textbox in the panel  when you submit the data . Here is the demo. I hope it could help you.


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <form id="form1" runat="server">
                <div class="form-group">
                <asp:Label ID="NumberOfFilesLabel" runat="server" Text="Number of Textbox" CssClass="control-label col-md-2" />
                <div class="col-md-10">
                    <asp:TextBox ID="NumberOfFilesValue" runat="server" OnTextChanged="NumberOfFilesValue_TextChanged" AutoPostBack="true" CssClass="form-control" style="width:50px" />
            <asp:Panel ID="FilenamePlaceholder" runat="server" />
            <hr />
        <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <asp:Button ID="BatchSubmit" runat="server" Text="Destroy" OnClick="BatchSubmit_Click" CssClass="btn btn-default" />


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    namespace Class
        public partial class article : System.Web.UI.Page
            protected void Page_Load(object sender, EventArgs e)
                List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("textbox")).ToList();
                foreach (string key in keys)
                    addrow tbRow = new addrow();
            protected void NumberOfFilesValue_TextChanged(object sender, EventArgs e)
                int file_count = Convert.ToInt16(NumberOfFilesValue.Text);
                string pre = "text";
                for (int i = 1; i <= file_count; i++)
                    string control_text = pre + i.ToString();
                    addrow tbRow = new addrow();
            protected void BatchSubmit_Click(object sender, EventArgs e)
                // loop for all textbox then submit the data finally delete them
        public class addrow
            public HtmlGenericControl NewRow(string ControlText)
                HtmlGenericControl main_div = new HtmlGenericControl("div"); //main div
                main_div.Attributes["class"] = "form-group";
                Label l = new Label
                    ID = ControlText.Replace(" ", String.Empty) + "Label",
                    Text = ControlText,            
                main_div.Controls.Add(l); //add label to main div
                HtmlGenericControl value_div = new HtmlGenericControl("div"); //secondary div
                value_div.Attributes["class"] = "col-md-10";
                TextBox tb = new TextBox
                    ID = ControlText.Replace(" ", String.Empty) + "Value",
                    CssClass = "form-control"
                main_div.Controls.Add(value_div); //add secondary div to main div
                return main_div;

    It shows as :

    Best Regards


    Friday, March 15, 2019 2:31 AM