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 02, 2005 9:11 PM by ScottGu

Comments

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

Wednesday, August 03, 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 03, 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 03, 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 03, 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 03, 2005 11:29 AM by Alex
<%@ Page ShowHeaders="false" %>

:)

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

Wednesday, August 03, 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 03, 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 03, 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 04, 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 04, 2005 5:17 PM by Matagatin
I can already see people using this little feature all the time from setting page loading behaviors to setting control and variables default. Is there a limit to the number of attributes a page can have?

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

Thursday, August 04, 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 05, 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 05, 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 04, 2006 5:35 AM by Dave Griffiths
Sorry Scott, to drag up a blog item from your past .. :-) These custom page attributes are a great feature, something I've wanted since the early days of classic ASP. So thanks to the team for that. One issue though .. In VS.NET 2005, while they work OK at runtime, there is a bit of a problem with the asp.net markup validation .. so you end up with an Error being generated for every custom attribute you use .. This is a bit of a pain, since you normally check the Error list for outstanding errors before doing a build/test. Is there anyway to re-flag these errors as warnings? Or better, can I add any other declaration code to allow the validator to pick up the custom properties from the page's base class. I suppose if we want to use a very useful, un-documented feature, I can live with the errors, but it would be nice to find a workaround.

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

Friday, August 04, 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 9:12 AM by Gabriel Lozano-Mor&#225;n
Scott, ignore my previous post I finally figured it out. I was receiving the HTML validation error that was discussed here as well. You commented with: "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" Has anything changed in the RTM version? I mean is there a better way to make sure the the HTML gets validated correctly? TIA Gabriel

# 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 01, 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 03, 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 03, 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 07, 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

# A Cool ASP.NET 2.0 Feature (Well, Almost)

Sunday, June 17, 2007 10:04 AM by Jedi Meditations

Whenever I start a new web project, one of the first things I do is create a "Base" class for all my...

# 在asp.net 2.0中结合母板页使用meta标签(扩展@Page指令)

Sunday, July 01, 2007 8:44 PM by 静水流深

介绍母板页是asp.net 2.0中的一个非常强大的特性,但是它却不能提供一个实现最基础的针对搜索引擎优化的方法。如果你想你的web页被搜索引擎收录并提高排名,那么你就需要在每一个页都指定一个title和meta标记。本文将说明如何扩展你的asp.net页,以使得在使用母板页的时候你可以直接在你内容页的@Page指令中指定你的meta标签的描述和meta标签的关键字。背景当你要针对搜索引擎优化你的web页的时候,设置页的title标签和页的meta描述是其中最重要的因素之一。&lt;title&gt;和meta标签实际上是在每个页的HTML的&lt;head&gt;部分

# sun valley resort

Sunday, September 07, 2008 3:25 AM by sun valley resort

But the moment he invoked his police authority to take them to jail, the standard changed, Desrochers said. At that point, he said, Lawrence should have placed the suspects under formal arrest for investigation of specific charges before moving them against