• Welcome to Valhalla Legends Archive.
 

Subclassing System.Web.UI.Page in ASP.NET 2.0

Started by MyndFyre, September 14, 2004, 11:00 PM

Previous topic - Next topic

MyndFyre

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!
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

quasi-modo

#1
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.
WAR EAGLE!
Quote(00:04:08) zdv17: yeah i quit doing that stuff cause it jacked up the power bill too much
(00:04:19) nick is a turtle: Right now im not paying the power bill though
(00:04:33) nick is a turtle: if i had to pay the electric bill
(00:04:47) nick is a turtle: id hibernate when i go to class
(00:04:57) nick is a turtle: or at least when i go to sleep
(00:08:50) zdv17: hibernating in class is cool.. esp. when you leave a drool puddle

MyndFyre

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

MyndFyre

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.