从动态TextBox中检索值

时间:2013-07-17 16:12:58

标签: c# asp.net textbox

我有Button动态创建TextBoxes列表,我也有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获取值。

提前谢谢!!

1 个答案:

答案 0 :(得分:0)

这样做

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