2009-06-25 34 views
0

我以前也提出过这个问题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(零)!

+0

is obj null or lblOrgName? – shahkalpesh 2009-06-25 06:34:33

回答

2

我把下面的页面行:

<%@ Register src="~/Controls/OrgInfo.ascx" TagName="OrgInfo" TagPrefix="proj1"/> 

和它的作品!

0

听起来好像控件“lblOrgName”试图分配一个值,但它尚未构建/解析到aspx页面。

请确保您有下面的一组有问题的aspx页面的@ Page指令:

AutoEventWireup="true" 
+0

是的。 Page Language =“C#”AutoEventWireup =“true” – TheVillageIdiot 2009-06-25 07:45:35

2

你怎么把你的用户控件在网页上?这在过去发生在我身上,原因是控件意外地被添加为服务器控件而不是用户控件。控制中的所有子控制都从未安装过。

您可以通过将在一个受保护的默认构造函数的代码隐藏的用户控件检查:

保护的MyUserControl(){}

这样的控制不能被实例化作为服务器控件。

+0

我编辑了这个问题。请进一步的建议?我把它写成这样 TheVillageIdiot 2009-06-25 08:21:35

相关问题