动态添加 控件 并获取值

动态添加控件:

 protected void Button1_Click(object sender, EventArgs e)
    {
        int column = GridView1.HeaderRow.Cells.Count;//得到GridView1列的方法
        for (int i = 0; i < column; i++)
        {
            string txtID = "New"+GridView1.HeaderRow.Cells[i].Text.ToString();
            TextBox tmp = new TextBox();
            tmp.ID = txtID;
            Form.FindControl("TD2").Controls.Add(tmp);
          
        }

    }

获取其值:

protected void Button2_Click(object sender, EventArgs e)//递交
    {
        Hashtable ht = new Hashtable();
        string table_name = Label1.Text;
        string[] ak = Request.Form.AllKeys;
        for (int i = 0; i < Request.Form.Count; i++)
        {
            //只筛选出动态生成的三个控件的值  
            if (ak[i].IndexOf("New") > -1)
            {
                string a = ak[i].Substring(3);
                ht.Add(ak[i].Substring(3), SqlStringConstructor.GetQuotedString(Request.Form[i]));
            }
               
        }

        db.Insert(table_name, ht);
        Gridview_Find();
    }

原文地址:https://www.cnblogs.com/tongdengquan/p/6090642.html