日期:2014-05-19  浏览次数:20424 次

在线等着给分!
学习用户控件时遇到下面问题:     “未将对象引用设置到对象的实例
----------------------------------
以下是主页面代码:
public   class   WebForm1   :   System.Web.UI.Page
{
protected   System.Web.UI.WebControls.TextBox   TextBox1;
protected   System.Web.UI.WebControls.Button   Button1;
protected   WebUserControl1   demo1;

private   void   Page_Load(object   sender,   System.EventArgs   e)
{
this.demo1.myClick+=new   WebApplication3.WebUserControl1.daili(demo1_myClick);
}

private   void   Button1_Click(object   sender,   System.EventArgs   e)
{
TextBox1.Text=this.demo1.Mytext;
}

private   void   demo1_myClick()
{
this.demo1.Mytext=TextBox1.Text;
}
}
这是用户控件代码:
public   class   WebUserControl1   :   System.Web.UI.UserControl
{
protected   System.Web.UI.WebControls.TextBox   TextBox1;
protected   System.Web.UI.WebControls.Button   Button1;
public   delegate   void   daili();
public   event   daili   myClick;

private   void   Page_Load(object   sender,   System.EventArgs   e)
{
//   在此处放置用户代码以初始化页面
}

public   string   Mytext
{
get
{
return   this.TextBox1.Text;
}
set
{
this.TextBox1.Text=value;
}
}

private   void   Button1_Click(object   sender,   System.EventArgs   e)
{
myClick();
}
}
----------------------------------
请问问题出在哪?解答后马上给分,谢谢!

------解决方案--------------------
报“未将对象引用设置到对象的实例”是指编译器不认识某个对象。
你可以挂断点单步调试。
------解决方案--------------------
protected WebUserControl1 demo1;
demo1并不是你页面的那个用户控件
用WebUserControl1 demo1 = (WebUserControl1 )this.Findcontrol( "你用户控件在你页面的ID ");
------解决方案--------------------
private void demo1_myClick()
{
this.demo1.Mytext=TextBox1.Text;
}

改成
static public void demo1_myClick()
{
this.demo1.Mytext=TextBox1.Text;
}

试试