【问题标题】:Get text value from dynamically creating text boxes从动态创建文本框获取文本值
【发布时间】:2014-06-29 21:36:04
【问题描述】:

给定多个动态创建的文本框,我想获取用户填写的文本。 我使用了面板和创作作品。找不到文本框的控件。

ASPX

<div>
  <asp:Panel ID="Panel1" runat="server"></asp:Panel>
</div>
<div>
  <asp:Button ID="Button1" runat="server" Text="Adauga autor" OnClick="Button1_Click" />
</div>

创建文本框

protected void Button1_Click(object sender, EventArgs e)
{
    int rowCount = Convert.ToInt32(Session["clicks"]);
    rowCount++;
    Session["clicks"] = rowCount;

    for (int i = 1; i <= rowCount; i++)
    {
        TextBox TxtBoxA = new TextBox();
        Label lblA = new Label();

        TxtBoxA.ID = "TextBoxA" + i.ToString();
        lblA.ID = "LabelA" + i.ToString();
        lblA.Text = "Label" + i.ToString() + ": ";

        Panel1.Controls.Add(lblA);
        Panel1.Controls.Add(TxtBoxA);
        Panel1.Controls.Add(new LiteralControl("<br />"));
    }
}

获取文本

int rowCount = Convert.ToInt32(Session["clicks"]);
for (int i = 1; i <= rowCount; i++)
{
    string item = "TextBoxA" + i.ToString();
    Control foundControl = FindControl(item, Page.Controls);
    TextBox TB = (TextBox)foundControl;
    string txt = TB.Text;
}

+

public static Control FindControl(string controlId, ControlCollection controls)
{
    foreach (Control control in controls)
    {
        if (control.ID == controlId)
            return control;

        if (control.HasControls())
        {
            Control nestedControl = FindControl(controlId, control.Controls);

            if (nestedControl != null)
                return nestedControl;
        }
    }
    return null;
}

文本框控件为空。我做错了什么?

【问题讨论】:

  • “获取文本”在哪里?您必须最迟在Page_Load 中重新创建所有控件。 Button1_Click 稍后。
  • 字符串 txt = TB.Text;但为什么是 Page_Load?只有在 Button1_Click 输入后我才需要文本。
  • 您必须在Page_initPage_Load 中重新创建所有动态创建的控件,稍后(如事件处理程序)对于ViewState 等来说为时已晚 . 另外,您的递归FindControl 是不必要的。在控件的NamingContainer 上使用“正常”FindControl。由于您不使用实现INamingContainer(如GridView))的控件,因此您可以简单地使用Page.FindControl(..)

标签: c# asp.net findcontrol nested-controls


【解决方案1】:

您最迟必须在Page_Load 中重新创建所有控件。 Button1_Click 为时已晚。所以在Page_Init/Page_Load 中重新创建Session["clicks"] 并在Button-Click-handler 中创建单个新控件。

一些代码:

protected void Page_Init(Object sender, EventArgs e)
{
    RecreateControls();
}

 private void RecreateControls()
 {
     int rowCount = Convert.ToInt32(Session["clicks"]);
     CreateControls(rowCount);
 }

 private void AddControl()
 {
     int rowCount = Convert.ToInt32(Session["clicks"]);
     CreateControls(1);
     Session["clicks"] = rowCount++;
 }

 private void CreateControls(int count)
 {
     for (int i = 1; i <= count; i++)
     {
         TextBox TxtBoxA = new TextBox();
         Label lblA = new Label();

         TxtBoxA.ID = "TextBoxA" + i.ToString();
         lblA.ID = "LabelA" + i.ToString();
         lblA.Text = "Label" + i.ToString() + ": ";

         Panel1.Controls.Add(lblA);
         Panel1.Controls.Add(TxtBoxA);
         Panel1.Controls.Add(new LiteralControl("<br />"));
     }
 }

 protected void Button1_Click(object sender, EventArgs e)
 {
     AddControl();
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    • 2017-01-12
    • 1970-01-01
    相关资源
    最近更新 更多