Dynamically added TextBoxes empty on button click

2019-07-25 17:50发布

I'm adding TextBoxes dynamically and when I click a submit button and have a postback, I can't see the values entered into the TextBoxes, all are coming up emtpy. Here's the .aspx page...\

form id="form1" runat="server">
        <asp:PlaceHolder ID="phFormContent" runat="server">

        </asp:PlaceHolder>
        <br /><br />
        <asp:Button ID="btnAddForm" runat="server" Text="Add Form" OnClick="btnAddForm_Click" />
        <asp:Button ID="btnSubmitForms" runat="server" Text="Submit Forms" OnClick="btnSubmit_Click" />
    </form>

...here's how I add the TextBoxes to the form on clicking btnAddForm...

protected void btnAddForm_Click(object sender, EventArgs e)
        {
            // Create Labels
            Label lblName = new Label();
            lblName.Text = "NAME:";
            Label lblNumber = new Label();
            lblNumber.Text = "NUMBER:";
            Label lblAddress = new Label();
            lblAddress.Text = "ADDRESS:";
            Label lblCompany = new Label();
            lblCompany.Text = "COMPANY:";

            // Create Text Boxes
            TextBox txtName = new TextBox();
            TextBox txtNumber = new TextBox();
            TextBox txtAddress = new TextBox();
            TextBox txtCompany = new TextBox();

            // Create submit button
            Button btnSubmit = new Button();
            btnSubmit.Text = "SUBMIT";

            // Create panel and add controls
            Panel pnlForm = new Panel();
            pnlForm.Controls.Add(lblName);
            pnlForm.Controls.Add(txtName);
            pnlForm.Controls.Add(new LiteralControl("<br /><br />"));
            pnlForm.Controls.Add(lblNumber);
            pnlForm.Controls.Add(txtNumber);
            pnlForm.Controls.Add(new LiteralControl("<br /><br />"));
            pnlForm.Controls.Add(lblAddress);
            pnlForm.Controls.Add(txtAddress);
            pnlForm.Controls.Add(new LiteralControl("<br /><br />"));
            pnlForm.Controls.Add(lblCompany);
            pnlForm.Controls.Add(txtCompany);
            pnlForm.Controls.Add(new LiteralControl("<hr />"));
            pnlForm.Controls.Add(new LiteralControl("<br /><br />"));
            panels.Add(pnlForm);

            foreach (Control panel in panels)
            {
                phFormContent.Controls.Add(panel);
            }
        }

...and here's how I try to extract the fields for each individual panel added...

private static void GetFormFields(Control panelControl)


   {
        ControlCollection controls = panelControl.Controls;
        foreach (Control childControl in panelControl.Controls)
        {
            if (childControl.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
            {
                TextBox txt = childControl as TextBox;
                fields.Add(txt);
            }
            else
            {
                GetFormFields(childControl);
            }
        }
    }

panels and fields are static List, each panel containing four fields. I pass GetFormFields an individual panel reference...

private static List<Control> panels = new List<Control>();
        private static List<TextBox> fields = new List<TextBox>();

2条回答
聊天终结者
2楼-- · 2019-07-25 18:47

Try dynamically adding them on the Page_Init event. Typically this will ensure they persist through PostBack. If you can't do this, you'll have to look at preserving their data manually by storing in ViewState.

查看更多
forever°为你锁心
3楼-- · 2019-07-25 18:53

Looks like the text boxes are not being included in the VIEWSTATE for the page, so are lost on postback.

There's some detail about what happens in this scenario here :

http://msdn.microsoft.com/en-us/library/kyt0fzt1(v=VS.71).aspx

查看更多
登录 后发表回答