我现在在后台生成一个TextBox,默认有个初始值我在页面上还有一个按钮,我怎么才能按下这个按钮之后,在后台获得,这个TextBox修改值
                   private void Page_Load(object sender, System.EventArgs e)
{
TableRow tr=new TableRow(); 
TableCell tc1=new TableCell(); 
TextBox t=new TextBox(); 
t.ID = "tb"+Table1.Rows.Count; 
                           t.Text = "tb"+Table1.Rows.Count.ToString();
tc1.Controls.Add(t); 
TableCell tc2=new TableCell(); 
DropDownList dpl=new DropDownList(); 
dpl.ID="dpl"+Table1.Rows.Count; 
for(int i=0;i<10;i++)dpl.Items.Add(i.ToString()); 
tc2.Controls.Add(dpl); 
tr.Cells.Add(tc1); 
tr.Cells.Add(tc2); 
Table1.Rows.Add(tr); 
                   }                  private void Button1_Click(object sender, System.EventArgs e)
{//在这里面我怎么获得前面动态生成的TextBox修改的内容,
                   //如果使用变量,在page_load里赋值获得是原始值                   }  

解决方案 »

  1.   

    Control c = this.Controls.FindControl("TextBox控件ID");
    if (c!= null)
       ((TextBox)c).Text = "asdf";
      

  2.   

    Control c = this.Controls.FindControl("TextBox控件ID");
    这句话写在那里
      

  3.   

    就写在Button1_Click事件里面啊。。
      

  4.   

    前台我只有一个table:Table1啊
    然后再后台给他添加控件Text
      private void Button1_Click(object sender, System.EventArgs e)
    {//在这里面我怎么获得前面动态生成的TextBox修改的内容,
                       //如果使用变量,在page_load里赋值获得是原始值
    Response.Write(((TextBox)this.FindControl("tb"+Table1.Rows.Count.ToString)).Text);
                       }
    我用这种方法也不行此时会提示我:
    异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
      

  5.   

    this.Controls.FindControl这种我这里怎么才能使用??
      

  6.   

    Control myControl1 = FindControl("TextBox");
    if(myControl1!=null)
    {
    Response.Write(((TextBox)myControl1).Text);
    }
    else
    {
    Response.Write("Control not found");
    }
    这样就可以了
      

  7.   

    private void Button1_Click(object sender, System.EventArgs e)
    {
    string strText =((TextBox)Table1.FindControl(Label1.Text)).Text;
    }private void Page_Load(object sender, System.EventArgs e)
    {TableRow tr=new TableRow(); 
    TableCell tc1=new TableCell(); System.Web.UI.WebControls.TextBox t=new TextBox(); 
    t.ID = "tb"+Table1.Rows.Count; 
    t.Text = "tb"+Table1.Rows.Count.ToString();
    tc1.Controls.Add(t); Label1.Text="tb"+Table1.Rows.Count.ToString();//记录ID
    TableCell tc2=new TableCell(); DropDownList dpl=new DropDownList(); 
    dpl.ID="dpl"+Table1.Rows.Count; 
    for(int i=0;i<10;i++)dpl.Items.Add(i.ToString()); 
    tc2.Controls.Add(dpl); 
    tr.Cells.Add(tc1); 
    tr.Cells.Add(tc2); 
    Table1.Rows.Add(tr); 
     }private void Button1_Click(object sender, System.EventArgs e)
    {
        string strText =((TextBox)Table1.FindControl(Label1.Text)).Text;
    }
      

  8.   

    加一个<ASP:LABEL id="Label1"  visible="False" runat="server"/>
      

  9.   

    恭喜楼主,我也分了一杯羹,呵呵,美阿! for(int i=0;i<15;i++)
    {
    TextBox txtName = new TextBox();
    txtName.ID="txtName"+i.ToString();
    string strTemp=txtName.ID.ToString(); 
    DataRow dr = ds.Tables[0].Rows[i];
    Control c = FindControl(strTemp);
    if (c!=null)
    {
    ((TextBox)c).Text =  dr["Name"].ToString();
    }
    }我这里的文本框是自定义好了的,然后循环负值而已,希望对其他人也能有些帮助