在一个动态生成的TextBox中输入值,如何实现在点击保存按钮后把输入到TextBox中的值保存下来???

解决方案 »

  1.   

    1、在CreateChildControls里new TextBox、设置ID,并添加到当前Control(或Page、UserControl)的Controls2、在OnLoad(或Page_Load)的!IsPostBack里初始化TextBox的内容3、在按钮事件处理方法中取值
      

  2.   

    保存按钮后把输入到TextBox中的值保存下来楼上说的很清楚了,直接取出数据保存就是了
      

  3.   

    在服務器端怎麼讀出數據?服務器根本就不能捕捉到這一TextBox控件!!
      

  4.   

    前几天刚做的一个实例,参考一下吧,页面上先拖一个table 
    protected void Page_Load(object sender, EventArgs e)
        {
            Table1.CellPadding = 0;
            Table1.CellSpacing = 0;
            Table1.Width = Unit.Percentage(50);        TableRow trHead = new TableRow();        TableCell tcHeadFoodName = new TableCell();
            TableCell tcHeadType = new TableCell();//Header
            tcHeadFoodName.Text ="类别";
            tcHeadType.Text = "值";
            trHead.Cells.Add(tcHeadFoodName);
            trHead.Cells.Add(tcHeadType);        trHead.HorizontalAlign = HorizontalAlign.Center;
     
            trHead.Height = 30;               TableRow tr2 = new TableRow();
            TableCell tc21 = new TableCell();
            TableCell tc22 = new TableCell();
            tc21.Text = "产品名称:";
            TextBox tb1 = new TextBox();
            tb1.ID = "txtProdName";
            tc22.Controls.Add(tb1);
            tr2.Cells.Add(tc21);
            tr2.Cells.Add(tc22);
            Table1.Rows.Add(tr2);
    //从库里读出
            SqlConnection conn = DB.CreateConn();
            conn.Open();
            int pid=Convert.ToInt32(Request.QueryString["classid"].ToString());
            lblClassid.Text = Request.QueryString["classid"].ToString();
            string sql = "select  t.* from(select * from tb_class where parentid=" +pid+ " and isclass=0 union all select * from tb_class where parentid in(select classID from tb_class where parentid=3 and isclass=0)) t order by dbo.f_getClassmergid(t.classid)";
            SqlCommand cmd = new SqlCommand(sql,conn);
            SqlDataReader dr = cmd.ExecuteReader();        while (dr.Read())
            {//开始动态创建控件
                if (Convert.ToInt32(dr["parentid"].ToString()) == pid)
                {//判断是类别,则不创建控件。
                    TableRow tr1 = new TableRow();
                    TableCell tc1 = new TableCell();
                    TableCell tc2 = new TableCell();
                    tc1.Text = dr["className"].ToString();
                    tr1.BackColor = Color.LightGray;                tr1.Cells.Add(tc1);
                    tr1.Cells.Add(tc2);
                    Table1.Rows.Add(tr1);
                }
                else
                {
                    TableRow tr1 = new TableRow();
                    TableCell tc1 = new TableCell();
                    TableCell tc2 = new TableCell();                Label lbl = new Label();
                    lbl.ID = "lbl" + dr["classid"].ToString();
                    lbl.Text = dr["className"].ToString();                TextBox tb = new TextBox();
                    tb.ID = "txt" + dr["classid"].ToString();  //预先设置ID                                tc1.Controls.Add(lbl);
                    tc2.Controls.Add(tb);
                    //验证控件
                    RequiredFieldValidator rf = new RequiredFieldValidator();
                    rf.ID = "rs" + dr["classid"].ToString();
                    rf.ControlToValidate = "txt" + dr["classid"].ToString();
                    rf.ErrorMessage = "请输入"+dr["className"].ToString();                tc2.Controls.Add(rf);                tr1.Cells.Add(tc1);
                    tr1.Cells.Add(tc2);
                    tr1.HorizontalAlign = HorizontalAlign.Left;
                    tr1.Height = 30;
                    tr1.VerticalAlign = VerticalAlign.Middle;                Table1.Rows.Add(tr1);
                }
            }
            dr.Close();
            conn.Close();
            conn.Dispose();
        }
        protected void Button1_Click(object sender, EventArgs e)   ///取值时
        {
            this.Label1.Text = ("名称:" + ((TextBox)(Table1.FindControl("txtProdName"))).Text + "<br>");
            SqlConnection conn = DB.CreateConn();
            conn.Open();
            string sql = "select * from tb_class where parentid in(select classID from tb_class where parentid="+Convert.ToInt32(lblClassid.Text)+" and isclass=0) order by dbo.f_getClassmergid(classid)";
            
            SqlCommand cmd1 = new SqlCommand(sql, conn);
            SqlDataReader dr = cmd1.ExecuteReader();
            while (dr.Read())
            {  //真正的取值的字段,要FindControl一下.
                this.Label1.Text += ((Label)Table1.FindControl("lbl"+dr["classid"].ToString())).Text+((TextBox)Table1.FindControl("txt" + dr["classid"].ToString())).Text+"<br>";
            }
            dr.Close();
            conn.Close();
            conn.Dispose();    }
      

  5.   

    路过------------------------
    http://fenglin.xland.cn
    ------------------------