This might sound like an odd question, but when does a Page object actually
come into existence? Let me explain a little bit of the background
information for you. From my understanding, currently your code-behinds for
your pages inherit from System.Web.UI.Page as do your User
Controls(Pagelets). However one of the planned breaking changes is that
User Controls will inherit from System.Web.UI.Control, which interestingly
enough is what Page inherits from right now. Combine this with a desire to
make certain enhancements to the Page class, I've created two classes for my
personal use: MyPage, which inherits from System.Web.UI.Page and
MyUserControl which also inherits from Page. I have also learned that User
Controls cannot talk to each other directly, however they can communicate
with the Page they are on, and the Page can communicate with them. So
inside MyUserControl I've overwritten the Parent property of Page to return
me a reference to the page that I currently am on, which looks like this.
public new MyPage Parent
{
get
{
Control control = null;
try
{
control = base.Parent;
Trace.Write( control.ToString(), "Try
base.Parent.ToString();" );
}
catch( Exception e )
{
Trace.Write( e.ToString(), "Catch Exceptions to
control = base.Parent" );
}
while ( ! ( control is Page ) )
{
try
{
control = control.Parent;
Trace.Write( control.ToString(),
"control.Parent.ToString()" );
}
catch( Exception e )
{
Trace.Write( e.ToString(), "Catch Exceptions
to control = control.Parent" );
break;
}
}
return ( MyPage )control;
}
}
Now I have had this working for a while, the most notable example is a Menu
User Control that displays the Navigation system for the site, by accepting
values from other controls hosted on the page. However, recently I have
decided that in an effort to reduce the amount of copying HTML, and to have
a single code-base for all html, I would break up each page even more, to
have controls host the major pieces of the 'static' HTML bits. This is
where I'm starting to have problems.
I've created a PageStart User Control, that is suppose to contain everything
from the <!DOCTYPE> tag down to the first opening <table> tag, taking
attributes such as title.
<MyControls:PageStart Title="Welcome Page" StyleSheet="MyStyle.css"
ScriptLibrary="MyScripts.js" DocID="DocumentID" runat="server" />
This control, which is the first on the page, cannot access a Parent of any
type, it returns a NullReferenceException. Later controls however don't
have this problem. It is critical that this control access the MyPage class
instance though, because I am actually using it to set some of those values
as Properties of the Page. These errors lead me to believe that somewhere
in the opening HTML tags the Page Class is instantiated, and I think it's in
the body tag. Can anyone confirm this, or does anyone know a way around the
problem.
Thanks for the help,
Aaron