Obscure but cool feature in ASP.NET 2.0

I learned about a neat little feature that I didn't know existed in ASP.NET 2.0 today that I thought I'd pass on.  It is a way to extend the attributes supported by the <%@ Page %> directive at the top of a .aspx page.  Previously the supported page directive attributes were hard-coded and the parser only supported the specific ones that ASP.NET knew about out of the box.  With ASP.NET 2.0, if you declare a base-class with a public property, you can now set it using a page directive attribute. 

To see this in action, save the below class as "MyBase.cs" in your app_code directory:

using System;

public class BasePage : System.Web.UI.Page {


    private string message = "blank";

    public string Message {

        get {
            return message;
        }

        set {
            message = value;
        }
    }

    protected override void OnLoad(EventArgs e) {
        Response.Write("My Message: " + message);
    }
}

and then save the below page as "test.aspx":

<%@ Page Language="C#" message="My Test Message String"  Inherits="BasePage" %>

<html>
   <body>

   </body>
</html>

When you run it you'll get the message "My Test Message String" rendered.

Note: we are using this feature with Atlas which is how I learned about it.

 

Published Tuesday, August 2, 2005 9:11 PM by ScottGu

Comments

# re: Obscure but cool feature in ASP.NET 2.0

Wednesday, August 3, 2005 3:08 AM by Plip
Intresting - where do you see this being used?

Phil.

# re: Obscure but cool feature in ASP.NET 2.0

Wednesday, August 3, 2005 8:42 AM by Ramon Smits
Well this is possible with ASP.NET with normal controls. But you discovered that it is also possible with the class that represents the page. Interesting.. although I cannot really come up with a usable scenario :)

# re: Obscure but cool feature in ASP.NET 2.0

Wednesday, August 3, 2005 9:30 AM by Uwe
And how would the behaviour be, if I have a property like "Inherits" or "Language" or another reserved keyword?

# re: Obscure but cool feature in ASP.NET 2.0

Wednesday, August 3, 2005 10:19 AM by M. Keith Warren
I dont know what is cooler, the feature itself or the fact that there are so many new things you are just now learning about one of them.

# re: Obscure but cool feature in ASP.NET 2.0

Wednesday, August 3, 2005 11:29 AM by Alex
<%@ Page ShowHeaders="false" %>

:)

# re: Obscure but cool feature in ASP.NET 2.0

Wednesday, August 3, 2005 11:58 AM by Erik Porter
This is totally awesome! I wished this was possible in 1.1 soooo many times. I wanted it when I was doing my own Title Property (but this is irrelivant now that you guys have that already in 2.0). Alex's example is another good one. I'm sure more examples will come.

# re: Obscure but cool feature in ASP.NET 2.0

Wednesday, August 3, 2005 3:38 PM by Brennan Stehling
I have a perfect use for this feature. I have to disable some controls on a page on certain pages, like the homepage uses the same master page as every other page, but just on the home page I have to disable to header navigation control. I was doing that by setting a boolean value in the BasePage I already had in place. By doing it this way I could cut down on the code in the code-behind file.

But I just tried this with my ASP.NET 2 project and it did not work. I am working with VB.NET so maybe that is the difference.

# re: Obscure but cool feature in ASP.NET 2.0

Wednesday, August 3, 2005 4:28 PM by scottgu
Hi Brennan,

There is some weirdness with Beta2 in that if you derive from a code-behind file, the base class isn't picked up (since it is defined in the code-behind and hasn't been compiled yet) and so you can't set properties that way.

With the final RTM version there will be an extra attribute you'll be able to set on the page directive which will allow you to identify the base class and fix this.

Hope this helps,

Scott

# re: Obscure but cool feature in ASP.NET 2.0

Thursday, August 4, 2005 6:40 AM by LukCAD
Hello Scott!
It is really nice to have custom parameters(attributes) for Page directive. But from this possability follow next wishes - it is to have a lot of selectable Inheritance for one page. Because it will permit realize fully dynamic and fast changing the source code for page. It will mean for future that everyone will can create a lot asp.net applications for one start page for the same dns and increase the profit from hosting domain. For example: in nigth time site will have source code of site that will permit to show films for clients of site, in a day time it will business site for exchanging money for example.
Sincerely, LukCAD

# re: Obscure but cool feature in ASP.NET 2.0

Thursday, August 4, 2005 5:24 PM by scottgu
Hi Matagatin,

There are no limits to the number of attributes you can use.

Thanks,

Scott

# re: Obscure but cool feature in ASP.NET 2.0

Friday, August 5, 2005 4:00 AM by lexp
Do you guys (msft) understand that by adding this feature it will be possible in the next releases of asp.net to add attributes that do not exactly map to properties of Page?

# re: Obscure but cool feature in ASP.NET 2.0

Friday, August 5, 2005 8:44 AM by Blaster101
Now,it's no longer obscure... You'll be amazed that there are a lot of possible features available. Seek and you''l discover it. Heh heh

# re: Obscure but cool feature in ASP.NET 2.0

Friday, August 4, 2006 2:07 PM by ScottGu

Hi Dave,

I believe you could modify this behavior in one of two ways:

1) Opening up and extending the validation files that VS uses.  This blog post might be useful in doing this: http://weblogs.asp.net/scottgu/archive/2006/02/02/437228.aspx

2) Turn off HTML validation.  This blog post describes how: http://weblogs.asp.net/scottgu/archive/2005/11/23/431350.aspx

Hope this helps,

Scott

# re: Obscure but cool feature in ASP.NET 2.0

Friday, August 18, 2006 4:24 PM by Dave
Is there anything remotely close to doing this in 1.1? Such as setting a flag or attribute in the .aspx and have it accessible in the codebehind?

# re: Obscure but cool feature in ASP.NET 2.0

Friday, August 18, 2006 4:59 PM by ScottGu

Hi Dave,

Unfortunately you can't modify the <%@ Page %> directive in V1.1.

What you could look to-do, though, is to create a custom control that you place in the .aspx page and allows designers to set meta-data property there.  You could then retrieve it from your code-behind file at runtime to use.

Hope this helps,

Scott

# re: Obscure but cool feature in ASP.NET 2.0

Tuesday, October 10, 2006 8:59 AM by Gabriel Lozano-Mor&#225;n
Hello Scott, I have the following scenario: a BasePage with a boolean property "IsLocalized". Then I have a Default.aspx page whose class derives from this BasePage. I need to get the property "IsLocalized" in the page declaritive. It works fine as long as I place it directly in the _default class but not if the _default class derives from my BasePage.

# re: Obscure but cool feature in ASP.NET 2.0

Tuesday, October 10, 2006 11:17 AM by ScottGu

Hi Gabriel,

Have you set the CodeFileBaseClass property on the page directive to point at your base class?  You might also need to-do this to make it work with web site projects.

Thanks,

Scott

# re: Obscure but cool feature in ASP.NET 2.0

Wednesday, October 11, 2006 3:26 PM by Gabriel Lozano-Mor&#225;n
Hello Scott, Yes I did but the error I had was about the XHTML validation. Is there a workaround for this instead of extending the validation files? Thx G.

# re: Obscure but cool feature in ASP.NET 2.0

Wednesday, October 11, 2006 11:57 PM by ScottGu

Hi Gabriel,

Unfortunately I think the only way to fix this is to extend the validation schema file.

Sorry!

Scott

# re: Obscure but cool feature in ASP.NET 2.0

Friday, December 1, 2006 2:41 PM by graphex

This is very useful. My client is requesting meta tags (description, keywords) and I'm putting them on the pages with these. It would be great if Microsoft could update VS so that it didn't hijack my Description attribute for no good reason, and so that it could look at the CodeFileBaseClass parameters during validation. Oh well, guess it is time to write a schema...

# re: Obscure but cool feature in ASP.NET 2.0

Tuesday, December 19, 2006 8:26 AM by Vinay

This works only when I remove the codefile attribute from page attribute. Why?????

# re: Obscure but cool feature in ASP.NET 2.0

Wednesday, January 3, 2007 3:22 AM by Andreas Hengstebeck

Hi Scott,

is it also possible to create a new directive? <% MyDirective ... %> for example?

# re: Obscure but cool feature in ASP.NET 2.0

Wednesday, January 3, 2007 9:23 AM by ScottGu

Hi Andreas,

Unfortunately there is no way to create an entirely new directive for a page - you can only add attributes to existing ones I'm afraid.

Sorry,

Scott

# re: Obscure but cool feature in ASP.NET 2.0

Friday, January 12, 2007 7:50 PM by graphex

Any idea if this might be a little more "legalized" in the new service pack? I hate validation errors, but when I tried to edit my schema file to get rid of them, I was noticing all sorts of other (very odd) problems with validation.

# re: Obscure but cool feature in ASP.NET 2.0

Friday, January 26, 2007 7:56 PM by Johnson

Is there a way to add this feature to an existing page with a code file?  (<% Page Language="C#" Inherits="_default" CodeFile="default.aspx.cs" %>)  So I want to keep my existing inheritance and add the BasePage Inheritance.

Is this possible?  I know that you can only have one inheritance and multiple interfaces.

# re: Obscure but cool feature in ASP.NET 2.0

Wednesday, February 7, 2007 4:31 AM by sbyard

When using code-behind files and your own namespaces, you will also need to include the @Page attribute

CodeFileBaseClass="YourNamespace.YourBaseClass"

Assuming a namespace of just one level deep