All controls are null within usercontrol
Asked Answered
L

4

23

I have a UserControl which uses a UserControl, among other controls.

In the ascx file I have the following code:

<%@ 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:

test.Text = "Hello World!";

This actually sets the Text property of a literal control in my user control test.

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.

Am I not adding the user control correctly? Should I - or do I have to - specify the Src property when registering a Tag? 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.

Lowpitched answered 16/11, 2011 at 23:40 Comment(0)
R
29

If the user control is in your current project, then you need to include the src in the register statement:

<%@ 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:

<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. 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. At runtime, this will result in the designer file property being 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.

Rotarian answered 17/11, 2011 at 1:5 Comment(1)
Thanks @Rotarian The thing I noticed about registering controls in the web.config is if a control is a composite control of controls within the same directory - there will be an error. If anyone can explain that to me, I'd love to know why that is.Lowpitched
L
8

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.

        //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);
Larcher answered 19/1, 2018 at 15:26 Comment(1)
This worked for me. I had already added the control via the web.config but that wasn't enough on it's own in my case.Recognizee
V
5

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. 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.

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;
}
Vance answered 17/11, 2011 at 1:28 Comment(0)
W
1

Please try to put code in Page_prerender event of page. It will work for you.

Weekley answered 17/11, 2011 at 11:44 Comment(2)
The problem with using PreRender for populating controls is if there are any events on the control that interact with other controls, the data will be missing unless populated during load. If you populate your controls during load you will have that data accessible during preRender. Load -> Events Fire -> PreRender. PreRender should be used when you want to make sure the data is not stale (perhaps changed by an event).Vance
Note that Page.PreRender occurs after page.Load and any controls on page firing their Load events... prior to Page actually rendering. It is last chance to bind data to controls.Macdonald

© 2022 - 2024 — McMap. All rights reserved.