2

I have a UserControl (ascx) that, depending on the user's credentials, will load another UserControl (ascx). Currently the control to be loaded, contains a special navigation menu.

I am using this code:

UserControl jmNav = 
    (UserControl)Page.LoadControl("~/controls/client/jmNavigation.ascx");

Then, after some more code, I'm telling it to load, like this:

    SBarTopWelcome.Controls.Add(jmNav);

The problem is, that I'm getting an "object reference not set to instance of an object" error.

Yes, the path is correct - as I tried it like this, as well (in all variations):

UserControl jmNav = (UserControl)Page.LoadControl("/client/jmNavigation.ascx");

This one (and its variants) tells me it doesn't exist.

So! Any thoughts?

Mark Bogner
  • 451
  • 1
  • 5
  • 19
  • This one's a classic "where do I create my controls" question. From what event are you calling `UserControl jmNav = (UserControl)Page.LoadControl("~/controls/client/jmNavigation.ascx)`? – DiskJunky Feb 19 '13 at 20:48
  • I have a "protected void" that's loading everything from labels to css classes, as well as that user control - it's being fired on pageload. – Mark Bogner Feb 19 '13 at 20:50
  • Have you added a `Register` line to your markup? – gunr2171 Feb 19 '13 at 20:51
  • I've tried that - but on other pages, it wasn't necessary - though, those were on pages (aspx) and not UserControls (ascx). I even did a "<%@Reference control="path" %> – Mark Bogner Feb 19 '13 at 20:52
  • Are you able to strongly-define the user control, or are you just using base classes (like `Control`)? – gunr2171 Feb 19 '13 at 20:54
  • Just like I did it above....at least, I think that's what you meant by define... – Mark Bogner Feb 19 '13 at 20:56
  • I guess what i'm asking is is there a class named `UserControl`? Can you access specific methods and members from that class with your variable, or are you using ASP.NET base classes for web controls. – gunr2171 Feb 19 '13 at 20:58
  • Ah, ok... I'm using "UserControl" under this "using System.Web.UI;" – Mark Bogner Feb 19 '13 at 21:00

3 Answers3

1

One helpful solution is to add <%@ Register %> to your parent control. Yes, I know it's in your parent page, but it should also be in your control.

If you do this, you should be able to Strongly-Type your control. For example, a control with a class name of MyControl would be:

MyControl controlVar = (MyControl)this.LoadControl("MyControl.aspx");

If you are able to get the stronly-defined variable, you should have no problems.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
1

Inside SideBar.ascx add a place holder named SideBarTopWelcomePlaceHolder.

<asp:PlaceHolder ID="SideBarTopWelcomePlaceHolder" runat="server"/>

Then load jmNavigation UserControl to SideBarTopWelcomePlaceHolder like this.

Control jmNav = 
    Control Page.LoadControl("~/controls/client/jmNavigation.ascx");
SideBarTopWelcomePlaceHolder.Add(jmNav);
Win
  • 61,100
  • 13
  • 102
  • 181
  • That's cool, but - I'm using this: public Control SBarTopWelcome { get{ return Page.FindControl("SideBarTopWelcome");} } This is giving me "SBarTopWelcome" - does that make a difference? – Mark Bogner Feb 19 '13 at 21:10
  • No. As long as SideBarTopWelcome is not null, you can add control to PlaceHolder of SideBarTopWelcome. – Win Feb 19 '13 at 21:11
  • I updated the answer. You need to cast to SideBarTopWelcome control explicitly in order to access the PlaceHolder. – Win Feb 19 '13 at 21:25
  • "public SideBarTopWelcome SBarTopWelcome" can't happen that way. SideBarTopWelcome is a
    (id given, and runat server added) - this works for other pages - just don't know why it doesn't work here.
    – Mark Bogner Feb 19 '13 at 21:44
  • I'm a bit confuse. What is the name of two UserControls and where is SideBarTopWelcome located inside? Normally, we use Panel or PanelHolder instead of Div tag with runat=server. – Win Feb 19 '13 at 22:01
  • "SideBar.ascx" is the one that holds "SideBarTopWelcome" (which is a server side DIV). The one I'm adding to SideBarTopWelcome, is the "jmNavigation.ascx control". – Mark Bogner Feb 19 '13 at 22:08
  • Basically, you are adding jmNavigation.ascx into SideBarTopWelcome. It all happens inside SideBar.ascx. If so, making SideBarTopWelcome as Panel or PlaceHolder will solve your problem. I updated my answer. – Win Feb 19 '13 at 22:26
1

HA! I'm such a DORK! I was declaring the "SideBarWelcome" within a control like this:

public Control sideBarTopWelcome
{
    get { return Page.FindControl("SideBarTopWelcome"); }
}

When I should have done it like this:

public Control sideBarTopWelcome
{
    get { return FindControl("SideBarTopWelcome"); }
}

Without Page. Thank you anyway, guys. I appreciate it.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Mark Bogner
  • 451
  • 1
  • 5
  • 19