1

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>();
3
  • Are you able to see the textboxes you've added, and the values are just blank, or you're not able to see the dynamically added textboxes at all? Commented Jul 10, 2013 at 21:59
  • I'm able to see the textboxes just fine, it's only that they're Text property is an empty string, while the page clearly has text in the corresponding text input. Commented Jul 10, 2013 at 22:00
  • 1
    Do you have ViewState enabled for your page? You could try manually setting the control ViewstateMode when you add them. Something like: txtName.ViewStateMode = System.Web.UI.ViewStateMode.Enabled; Commented Jul 10, 2013 at 22:10

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I can't do this since I need the textboxes to be associated with a button click event. Do you know of any good info on adding the textbox data to the viewstate manually? I still don't really understand why this needs to be done but I've already tried setting EnableViewState to true for enclosing controls and that hasn't changed anything. Thanks.
There's a difference between programmatically-added controls and controls that exist in markup. What if you add these controls to the markup, hide them, and then display them on button click? This will avoid you needing to manually maintain the controls. Doing dynamic control generation is not fun.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.