Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从动态创建文本框中获取文本值_C#_Asp.net_Findcontrol_Nested Controls - Fatal编程技术网

C# 从动态创建文本框中获取文本值

C# 从动态创建文本框中获取文本值,c#,asp.net,findcontrol,nested-controls,C#,Asp.net,Findcontrol,Nested Controls,给定多个动态创建的文本框,我想让用户填写文本。 我使用了一个面板,创作效果很好。找不到文本框的控件 ASPX Textbox控件为空。我做错了什么?您必须最迟在页面加载中重新创建所有控件按钮1\u单击为时已晚。因此,在页面初始化/页面加载中重新创建会话[“单击”],并在按钮单击-处理程序中创建单个新控件 一些代码: protected void Page_Init(Object sender, EventArgs e) { RecreateControls(); } private

给定多个动态创建的文本框,我想让用户填写文本。 我使用了一个面板,创作效果很好。找不到文本框的控件

ASPX


Textbox控件为空。我做错了什么?

您必须最迟在
页面加载中重新创建所有控件<代码>按钮1\u单击
为时已晚。因此,在
页面初始化
/
页面加载
中重新创建
会话[“单击”]
,并在
按钮单击
-处理程序中创建单个新控件

一些代码:

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();
 }
受保护的无效页\u Init(对象发送方,事件参数e)
{
重新创建控件();
}
私有void重新创建控件()
{
int rowCount=Convert.ToInt32(会话[“单击]);
创建控件(行计数);
}
私有void AddControl()
{
int rowCount=Convert.ToInt32(会话[“单击]);
创建控件(1);
会话[“单击”]=行计数++;
}
私有void CreateControls(整数计数)
{

for(int i=1;i其中是“获取文本”定位?您必须最晚在
页面加载
中重新创建所有控件。
按钮单击
更晚。string txt=TB.Text;但是为什么页面加载?我只需要在按钮单击输入后才需要文本。您必须稍后在
页面初始
页面加载
中重新创建所有动态创建的控件(如事件处理程序)对于
ViewState
等来说已经太晚了。此外,您的递归
FindControl
是不必要的。请使用控件的
NamingContainer
上的“普通”FindControl
。由于您不使用实现
INamingContainer
(如
GridView)
的控件,您只需使用
页面。FindControl(…)
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;
}
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();
 }