views:

20

answers:

1

TL;DR: All controls within a usercontrol that's being used outside it's home project are null when that usercontrol's Page_Init/Page_Load methods are called.

The setup is like this:

Projects "UI.Frontend", "UI.ControlPanel", and "UI.Common" are "ASP.NET Web Application"s. UI.Common is never meant to be accessed directly- it just contains UserControls that are needed in both the frontend and the control panel.

So, an aspx file (SomeFrontendPage.aspx) in UI.Frontend contains the lines:

<%@ Register tagprefix="BP" Namespace="UI.Common" Assembly="UI.Common" %>

and later:

<BP:MyControl runat="server" ID="ctlMyControl" />

while over in UI.Common, there's a control named MyControl (normal ascx, ascx.cs, and ascx.designer.cs files). Now, when I open SomeFrontendPage.aspx in a browser, ctlMyControl gets loaded and it's init+load methods get executed. The problem is all subcontrols of MyControl never get initialized. Example (if MyControl.ascx has a textfield of ID txtBlah):

    protected void Page_Init(object sender, EventArgs e)
    {
        txtBlah.Text = "test";
    }

The above code will run, but will cause a null pointer (well, "Object reference not set to an instance of an object") since txtBlah will be null.

Edit: An example control would be:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl.ascx.cs" Inherits="Common.MyControl" %>
Whatever: <asp:TextBox ID="txtWhatever" runat="server"  />
A: 

You may find you have more problems that what is immediately shown. ASCX files aren't embedded in assemblies by default, and when they are, you then need to create a virtual path provider to access them.

Can we see an example control?

Matthew Abbott
That may be it- I originally used the register assembly method because I couldn't find any way to share controls between projects without messing with virtual directories.After posting this question I created a new solution with two empty projects and the setup described above to try to isolate this issue from any other factors that might be present in my current work. Turns out that the cross-project thing doesn't matter- "@Register Assembly" doesn't work to allow cross-project usercontrols. :(
Fishtoaster