简体   繁体   English

usercontrol中的所有控件都为null

[英]All controls are null within usercontrol

I have a UserControl which uses a UserControl, among other controls. 我有一个UserControl,它使用UserControl,以及其他控件。

In the ascx file I have the following code: ascx文件中,我有以下代码:

<%@ Register TagPrefix="tag" Namespace="some.name.space" Assembly="some.assembly" %>
<tag:control ID="test" runat="server" />

In my Page_Load method, I try to set a property on test like so: 在我的Page_Load方法中,我尝试在测试中设置属性,如下所示:

test.Text = "Hello World!";

This actually sets the Text property of a literal control in my user control test . 这实际上在我的用户控件test设置了文字控件的Text属性。

This throws an exception: 这引发了一个异常:

Object reference not set to an instance of an object 你调用的对象是空的

When it tries to set the 当它试图设置

lblTest.Text = value; 

The object that is null is lblTest . null为null的对象是lblTest

Am I not adding the user control correctly? 我没有正确添加用户控件吗? Should I - or do I have to - specify the Src property when registering a Tag? 我应该 - 或者我必须 - 在注册标签时指定Src属性吗? If so, I'd have to register every usercontrol I use? 如果是这样,我必须注册我使用的每个用户控件?

This also results in no controls loading in usercontrol and all controls are null within usercontrol. 这也导致usercontrol中没有控件加载,并且usercontrol中的所有控件都为null。

If the user control is in your current project, then you need to include the src in the register statement: 如果用户控件在您当前的项目中,那么您需要在register语句中包含src:

<%@ Register TagPrefix="uc1" TagName="NavTop" Src="controls/NavTop.ascx" %>

However, if you use this user control in more than one page, then you can also register it in web.config: 但是,如果您在多个页面中使用此用户控件,那么您也可以在web.config中注册它:

<system.web>
  <pages>
    <controls>
      <add tagPrefix="uc1" tagName="NavTop" src="~/controls/NavTop.ascx" />
    </controls>
  </pages>
</system.web>

One other thing to be aware of: there are times when the visual studio designer does not "see" your changes to controls on the page if you only make the changes in source view. 还有一点需要注意:如果您只在源视图中进行更改,有时Visual Studio设计器不会“看到”您对页面控件的更改。 If you change a control name, for example, you could end up with a control with the new name in the ascx but a reference to a control with the old name in the designer file. 例如,如果更改控件名称,最终可能会在ascx中使用新名称控件,但在设计器文件中引用具有旧名称的控件。 At runtime, this will result in the designer file property being null. 在运行时,这将导致设计器文件属性为null。

After having been burnt by this a number of times, if I make any changes in source view, I either check to see that the designer file has been updated correctly or I switch to design view, make a minor change, then save the page/user control. 经过多次烧毁之后,如果我在源视图中进行了任何更改,我要么检查设计器文件是否已正确更新,要么切换到设计视图,进行微小更改,然后保存页面/用户控制。

I had this problem when I was adding a user control in the code behind the wrong way. 当我在错误的方式后面的代码中添加用户控件时,我遇到了这个问题。 You have to use the Page.LoadControl method to initialize the control you can't just use new. 您必须使用Page.LoadControl方法初始化您不能只使用new的控件。

        //WRONG
        UserControls.BingoCardPage bcp = new UserControls.BingoCardPage();
        form1.Controls.Add(bcp);
        //RIGHT
        UserControls.BingoCardPage bcp = (UserControls.BingoCardPage)Page.LoadControl("~/UserControls/BingoCardPage.ascx");
        form1.Controls.Add(bcp);

The issue here is usually due the the load mechanics of user controls, they load after the page typically. 这里的问题通常是由于用户控件的加载机制,它们通常在页面之后加载。 So as a result the controls have not yet been initialized on your usercontrol (causing the null ref) during the containing page_load method. 因此,在包含page_load方法期间,尚未在usercontrol上初始化控件(导致null ref)。 One way to work around this is to just create and set a property on the usercontrol and have the usercontrol wire-up/populate its own UI in its Page_Load method. 解决此问题的一种方法是在usercontrol上创建和设置属性,并让usercontrol在其Page_Load方法中连接/填充自己的UI。

Something like this: 像这样的东西:

//Page
protected void Page_Load(object sender, EventArgs e)
{
    test.Text = "Hello World!";
}

//User Control
public string Text {get; set;}

protected void Page_Load(object sender, EventArgs e)
{
    lblTest.Text = Text;
}

Please try to put code in Page_prerender event of page. 请尝试将代码放在Page_prerender事件页面中。 It will work for you. 它会对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM