Valhalla Legends Archive

Programming => Web Development => Topic started by: MyndFyre on September 14, 2004, 11:00 PM

Title: Subclassing System.Web.UI.Page in ASP.NET 2.0
Post by: MyndFyre on September 14, 2004, 11:00 PM
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:


//----------------------------------
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);
}
}
//----------------------------------

As you can see, such a class would be EXTREMELY advantageous in code reuse!

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


//----------------------------------
public partial class Default_aspx : DerivedPage
{
//...
}
//----------------------------------


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!
Title: Re:Subclassing System.Web.UI.Page in ASP.NET 2.0
Post by: quasi-modo on September 15, 2004, 03:32 PM
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.
Title: Re:Subclassing System.Web.UI.Page in ASP.NET 2.0
Post by: MyndFyre on September 15, 2004, 06:32 PM
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.
Title: Re:Subclassing System.Web.UI.Page in ASP.NET 2.0
Post by: MyndFyre on September 15, 2004, 09:24 PM
Update: I found out what was wrong.

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


<pages pageBaseType="DerivedPage" />


This changed the partial Page type emitted.