我做了留言板回复功能,回复页面也是采用repater里面包含了一个textbox和一个BUTTON 我通过按钮点击回复。
以下是我的代码 运行出错    protected void BtnRePlay_Click(object sender, EventArgs e)
    {
        TextBox tb = (TextBox)Repeater1.FindControl("ReplayMsg");
        string aaa = tb.Text;
 
    }未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
行 26:     {
行 27:         TextBox tb = (TextBox)Repeater1.FindControl("ReplayMsg");
行 28:         string aaa = tb.Text;行 29:  
行 30:     }请高手指点下 谢谢

解决方案 »

  1.   

    回复页面 只是select 一行数据
      

  2.   

    放在 ItemCommand事件里;
     TextBox tb  = this.repeater1.Items[itemIndex].FindControl("ReplayMsg") as  TextBox;
     if(td!=null)
    {
      if(!String.IsNullOrEmpty((td.Text)))
    {
           string aaa = tb.Text;
     
    }
    }
      

  3.   

    TextBox tb 需要先实例化,我猜tb这个控件是代码生成的吧

      TextBox tb = new TextBox();
      
      
     
      

  4.   


    CS0103: 当前上下文中不存在名称“td”找不到TD
      

  5.   

    TextBox tb = this.repeater1.Items[itemIndex].FindControl("ReplayMsg") as TextBox;
    if(tb!=null)
    {
    if(!String.IsNullOrEmpty((tb.Text)))
    {
    string aaa = tb.Text;}
    }
      

  6.   

    刚才是我打错了    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandSource.GetType() == typeof(Button))
            {
                if (((Button)e.CommandSource).CommandName == "btclick")
                {
                    TextBox tb = new TextBox();
                    tb = this.Repeater1.Items[e.Item.ItemIndex].FindControl("ReplayMsg") as TextBox;
                    if (tb != null)
                    {
                        if (!String.IsNullOrEmpty((tb.Text)))
                        {
                            string aaa = tb.Text;                    }
                    }
                }
            }
          }点击按钮后 Repeater1_ItemCommand 并不执行哟
      

  7.   

    程序倒是执行了 
        protected void BtnRePlay_Click(object sender, EventArgs e)
        {
            //TextBox tb = (TextBox)Repeater1.FindControl("ReplayMsg");
            //string aaa = tb.Text;
     
        }这个事件