Valhalla Legends Forums Archive | Web Development | Subclassing System.Web.UI.Page in ASP.NET 2.0

AuthorMessageTime
Myndfyr
Hey!

It appears that autogenerating pages in ASP.NET 2.0 has severely gimped the OOP-ability of web programmers using the platform.

I have a custom Cookie class that encapsulates fields of an HttpCookie for my website. The desired outcome was to do something to the effect of providing access to a Cookie instance to all of my Web Forms, as well as setting one of several different page templates based on a cookie value:

[code]
//----------------------------------
public class DerivedPage : System.Web.UI.Page
{
private Cookie m_ckyCookie;
public Cookie Cookie { get { return m_ckyCookie; } }

protected override void OnPreInit(EventArgs e) {
// inits default values if the HttpCookie is null.
m_ckyCookie = new Cookie(Request.Cookies[Cookie.CookieName]);
// selects master page based on cookie value.
switch (m_ckyCookie.SelectedSkin) {
case WebsiteSkins.Generic:
#if DEBUG
default:
#endif
if (m_ckyCookie.LoggedIn)
{
this.MasterPageFile = "./Masters/Generic/GenericLoggedIn.master";
} else {
this.MasterPageFile = "./Masters/Generic/GenericLoggedOut.master";
}
break;
}
}

protected override void OnUnload(EventArgs e) {
// deletes the old cookie and saves a new one to the response.
m_ckyCookie.SaveCookie(Response);
}
}
//----------------------------------
[/code]
As you can see, such a class would be EXTREMELY advantageous in code reuse!

However, when I try to do the following with codebehind:

[code]
//----------------------------------
public partial class Default_aspx : DerivedPage
{
//...
}
//----------------------------------
[/code]

I get an error that there is a generated file with a different derived type, specifically, System.Web.UI.Page.

Is there any way to change this behavior in ASP.NET 2.0? This was *extremely easy* and helpful in 1.0 and 1.1, and to take out the ability to subclass the Page class REALLY gimps the OOP factor, which was a major selling point of ASP.NET!
September 15, 2004, 4:00 AM
peofeoknight
I am still using 1.1, I need to get with the program I guess. I have not been doing anything new or anything at all lately because I have been so busy. Wish I could offer a solution.

Hey did they do anything with the cookieless session? Like can it detect when cookies are not enabled and switch over to cookieless? Or did they fix any of the session instabilities (like it ending early, or on end events not fireing on time)? Did they clean up the markup so you can have pages that are more valid and semantically correct? I am just wondering, I have not seen many good articles about it and have not had a chance to use it yet.
September 15, 2004, 8:32 PM
Myndfyr
All I know in re: to your post (because I'm not using cookieless sessions) is that all web controls by default emit XHTML 1.1 Transitional, with XHTML 1.0 Strict also an option.
September 15, 2004, 11:32 PM
Myndfyr
Update: I found out what was wrong.

There is a setting in web.config that can change this. In my case, I set:

[code]
<pages pageBaseType="DerivedPage" />
[/code]

This changed the partial Page type emitted.
September 16, 2004, 2:24 AM

Search