[RESOLVED]UserControl won't recognize Page type – reference issues galore

For some reason this problem has been plagueing me for a while now and I’ve always found a way to work around it, but now I’m stuck and hoping someone can help… 

I’ve got a web app that has several pages in the root of the site.  In a sub-folder called "controls" I have several UserControls that I use to render the majority of the content.  For reasons I’m not clear on, when I try to refer to a class in the web app
from a UserControl VIsual Studio can’t resolve the class.  I get the "Cannot resolve symbol ‘x’" error.  So I put everything into the same namespace to see if that would fix this.  So, for example, I have a aspx page called "Main" where the namespace is "SampleApp.Web". 
On that page I have a UserControl called "UC1", and that UserControl is also in the namespace "SampleApp.Web".  When I type "SampleApp.Web." in the Page_Load of UC1 in VS it only shows that UserControl type, nothing else.  I type "Main" and I get the symbol
resolution error.  I have several other pages and UserControls using the same namespace, but for some reason the UserControls can only see themselves, and nothing else.

Code sample:

 

MAIN.ASPX:
<%@ Page Language="C#" MasterPageFile="~/masters/MasterPageMain.master" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="SampleApp.Web.Main" %>
<%@ Register Src="controls/UC1.ascx" TagName="Ctrl1" TagPrefix="uc1" %>

MAIN.ASPX.CS:
namespace SampleApp.Web
{
    public partial class Main : Page
    {
	}
}

CONTROLS/UC1.ASCX.CS:
namespace SampleApp.Web
{
    public partial class UC1 : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var test = (Main)this.Page; // THIS gives me an error, can't see the "Main" type
        }
    }
}

Does anyone know what I might be doing wrong?  I am baffled by this one.  Thanks for your help!

Eddie

I thought the page that it was referring to was the page of the usercontrol, as in a usercontrol is basically a mini page in itself?

Generally you shouldn’t be trying to move up the chain in this manner because it restricts the possible reuses of the usercontrol if its tied to a parent.

If you need data to travel this way you should think about either adding public properties or custom events.

Then from the page level you either subscribe to the usercontrols events or set the properties so that when it comes time for it to run it has all the information already and waiting for it.

 

Leave a Reply