我以前也提出过这个问题here。我再次面临同样类型的问题。我有一个用户控件显示来自某个业务对象的信息。我已经在两页上使用它。在这两个页面中,我调用一个带有业务对象的方法来将用户控件上的控件(主要是asp标签)绑定到对象的属性。我在aspx页面的LoadComplete方法中调用这个方法。UserControl在一个页面中工作,但不在其他页面中
protected void Page_LoadComplete(object sender, EventArgs e)
{
control.LoadData(bussinessObject);
}
它在两个页面上工作正常。现在我有第三页,我使用相同的控件。现在,当我打电话LoadData在这个新的一页我得到NullReference例外:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
Source Error:
Line 91: lblOrgName.Text = obj.Name;
编辑:! lblOrgName是NULL :(请帮我
编辑2: 我通过踩在页面页面加载页面之后,调用Masterpage的Page_Load,然后调用问题控件的Page Load。在本页面加载函数的右括号(})停止时,我在立即窗口中输入了lblOrgName,它显示为null :(
然后在包含用户控件的页面的LoadComplete中,我停下来并在即时窗口中键入控件的名称。它将其中的所有标签和其他控件都显示为空。
编辑3:
我已经把受保护的构造在用户控件的建议由Stendhal的答案:
protected OrgInfo(){ }
现在,我得到这个错误:
Compiler Error Message: CS0122: 'Controls.OrgInfo.OrgInfo()' is inaccessible
due to its protection level.
项目根目录中的aspx页面和OrgInfo控件位于Controls文件夹中。
编辑4:
我试着在页面加载方法,但可惜Controls集合的计数创建的所有控制(主要是标签)为0(零)!
is obj null or lblOrgName? – shahkalpesh 2009-06-25 06:34:33