从动态文本框中检索值

本文关键字:检索 动态 文本 | 更新日期: 2023-09-27 18:23:42

我有一个动态创建TextBoxes列表的Button,还有一个提交信息的Button。但是,我不知道如何访问Textboxes的值。以下是代码:

        if (IsPostBack)
        {
            ViewState["count"] = Convert.ToInt32(ViewState["count"]) + 1;
            int Count = int.Parse(string.Format("{0}", ViewState["count"]));
            var lstTextBox = new List<TextBox>();
            for (int i = 0; i < Counter; i++)
            {
                TextBox txtbx = new TextBox();
                txtbx.ID = string.Format("txtbx{0}", i);
                // txtbx.AutoPostBack = true;
                lstTextBox.Add(txtbx);
                //txtbx.Text = "initial value";
            }
            Session["lstTextBox"] = lstTextBox;
        }
protected void Button1_Click(object sender, EventArgs e)
    {
        int total = Counter;
        for (int i = 0; i < total; i++)//Calls to createbox
            CreateTextBox(i);
        //Label1.Text = Counter.ToString();
        if (Counter == 4)
        {
            Button1.Visible = false;
        }
    }
 private int Counter
    {
        get { return Convert.ToInt32(ViewState["count"] ?? "0"); } //Fields button counter
        set { ViewState["count"] = value; }
    }
private void CreateTextBox(int j) //Creates the fields / cells
    {      
            var box = new TextBox();
            box.ID = "Textbox" + j;
            box.Text = "Textbox" + j;
            var c = new TableCell();
            c.Controls.Add(box);
            r.Cells.Add(c);
            table1.Rows.Add(r);
    }

如何让Button2获取值。

提前感谢!!

从动态文本框中检索值

这样做

 foreach(Control c in YourControlHolder.Controls)
 {
  if(c is TextBox)
   {
       //your code here.
   }
 }