protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TableRow tr = new TableRow();
            TableCell td = new TableCell();
            TextBox txt = new TextBox();
            txt.ID = "txt0";
            td.Controls.Add(txt);
            tr.Cells.Add(td);
            tabUserDefined.Rows.Add(tr);
        }
    }    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox txt = form1.FindControl("txt0") as TextBox;
        string ss = txt.Text;
    }
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Table ID="tabUserDefined" Width="100%" runat="server" CellPadding="1" CellSpacing="1" border="1">
        </asp:Table>
    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </form>
</body>当点击按钮时提示未将对象引用设置到实例,那应该怎么获取TextBox的值呢?

解决方案 »

  1.   

    你的TextBox在那里????
    我没看见
      

  2.   

    txt.ID = "txt0";
    改成
    txt.name = "txt0";
    看行不?
    我对asp控件不熟悉。
      

  3.   

    TextBox txt = form1.FindControl("txt0") as TextBox;
            string ss = txt.Text;=>
            string ss = Request.Form["txt0"];
    准确的写法应该是 Request.Form[txt0.UniqueID]
      

  4.   

    string ss = Request.Form["txt0"];
    txt0 在此不是id ,是取 html 元素的 name
      

  5.   

    用当前pagetextbox tb= this.findControl("id") as textbox;
      

  6.   

    谢谢,能取到
    要是有个RadioButton怎么取它的Text属性,怎么判断有没有选中呢?
    比如这样protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TableRow tr = new TableRow();
                TableCell td = new TableCell();
                TextBox txt = new TextBox();
                txt.ID = "txt0";
                RadioButton rb = new RadioButton();
                rb.ID = "rb";
                rb.Text = "a";
                td.Controls.Add(txt);
                td.Controls.Add(rb);
                tr.Cells.Add(td);
                tabUserDefined.Rows.Add(tr);
            }
        }
      

  7.   

    RadioButton Text 取不到它有意义的是 value 属性你在页面上只放一个 RadioButton ?
      

  8.   

    很少用 asp.net的 table  可能问题出在你的控件加载上面,但是如果是指为了去得到值的话   可以采用 string ss = Request.Form["txt0"].ToString().Trim();这种方式直接就可以取得到值。
      

  9.   

    在RaidioButonn中绑定value值 再通过hidden来取值
      

  10.   

    把你的 if(!IsPostback) 注释掉!
      

  11.   

            //if (!IsPostBack)
            {
                TableRow tr = new TableRow();
                TableCell td = new TableCell();
                TextBox txt = new TextBox();
                txt.ID = "txt0";
                td.Controls.Add(txt);
                tr.Cells.Add(td);
                tabUserDefined.Rows.Add(tr);
      

  12.   

    不能把if(!IsPostback) 注释掉
      

  13.   

    //if (!IsPostBack) //不要加上这个判定
            {
                TableRow tr = new TableRow();
                TableCell td = new TableCell();
                TextBox txt = new TextBox();
                txt.ID = "txt0";
                td.Controls.Add(txt);
                tr.Cells.Add(td);
                tabUserDefined.Rows.Add(tr);
            }
      

  14.   

    另外,在oninit事件中加入控件,这样就有viewstate了。