如何从我的动态文本框中检索数据?

How do I retrieve data from my dynamic textboxes?

我在我的应用程序中添加了一些动态 asp:TextBox:查看我的

protected void AddBoxes()
{
    counter++;
    TextBox tb = new TextBox();
    tb.ID = "Textbox" + counter;
    tb.TextMode = TextBoxMode.MultiLine;
    tb.Rows = 5;
    tb.CssClass = "larger_tb";
    LiteralControl linebreak = new LiteralControl("<br />");
    LiteralControl openLI = new LiteralControl("<li>");
    LiteralControl closeLI = new LiteralControl("</li>");
    PlaceHolder1.Controls.Add(openLI);
    PlaceHolder1.Controls.Add(tb);
    PlaceHolder1.Controls.Add(closeLI);
    PlaceHolder1.Controls.Add(linebreak);
    controlIdList.Add(tb.ID);
    ViewState["controlIdList"] = controlIdList;
}

现在我想将这些信息直接提取到标签中。我知道我需要使用循环,因为我不知道用户选择了多少。这是我试过的:

foreach (Control control in PlaceHolder1.Controls)
{
    for (int i = 0; i < counter; i++)
    {
        lblScope.Text = "<li>" + PlaceHolder1.Controls[i].ToString() +"</li>";
    }
} 

然而,所有这一切都是打印出来的System.Web.UI.LiteralControl。应该怎么做?

已编辑 这是我尝试时发生的事情:

foreach (Control control in PlaceHolder1.Controls)
{
    if (control is TextBox)
    {
        TextBox txt = (TextBox)control;
        lblScope.Text += string.Format("<li>{0}</li>", txt.Text);
    }
}

如何阻止代码打印用户创建的第一个文本框的名称?

试试这个:

lblScope.Text = "";

foreach (Control control in PlaceHolder1.Controls)
{
    if (control is TextBox)
    {
        TextBox txt = (TextBox)control;
        lblScope.Text += string.Format("<li>{0}</li>", txt.Text);
    }
}
private void ProcessAllControls(Control rootControl)
{
    foreach (Control childControl in rootControl.Controls)
    {
         if(childControl is TextBox)
         {  
              TextBox txt = (TextBox)childControl;             
              lblScope.Text += string.Format("<li>{0}</li>", txt.Text);
         }
         else
         {     
              ProcessAllControls(childControl);
         }   
    }
}

如果您在其他控件中嵌套了复选框,则需要使用递归 rootControl.You 将调用如下方法:

ProcessAllControls(PlaceHolder1);

我们在做什么:我们循环 PlaceHolder1 中的所有控件,如果控件是复选框,我们将向 PlaceHolder1 中的标签添加文本。如果不调用相同的方法来检查当前控件中是否有嵌套的复选框。这是为所有控件执行的。就像我说的,这叫做递归。