c# - 单击按钮时动态添加的文本框为空

标签 c# asp.net dynamic textbox webforms

我正在动态添加文本框,当我单击提交按钮并进行回发时,我看不到输入到文本框中的值,所有值都变成空的。这是 .aspx 页面...\

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>

...这是我在单击 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);
            }
        }

...下面是我尝试为添加的每个单独面板提取字段的方法...

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);
            }
        }
    }

面板和字段是静态列表,每个面板包含四个字段。我向 GetFormFields 传递了一个单独的面板引用...

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

最佳答案

尝试在 Page_Init 事件中动态添加它们。通常这将确保它们通过 PostBack 持续存在。如果您不能这样做,您将不得不考虑通过将数据存储在 ViewState 中来手动保存它们的数据。

关于c# - 单击按钮时动态添加的文本框为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17581616/

相关文章:

jquery - tinyMCE setContent - 动态(jQuery)添加文本区域

c# - Codedom 和字符串处理

javascript - 如何使用我的后端从客户端 JS 发送电子邮件

c# - 视觉 C# : How to find out *which* assembly or reference I am missing

c# - 如何从 Windows 8 Metro 应用程序中的 WebView 获取 HTML 元素 onClick?

c# - HttpRequestValidationException 显示 YSOD 尽管 customErrors 部分

c# - 使用 ASP.NET 和 C# 按页显示记录

c# - 如何通过将泛型类型传递给基类来触发扩展方法?

ASP.NET MVC 应用程序分析

mysql - 根据选中的复选框更改 sql 查询