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.

 

26 Comments

  • Intresting - where do you see this being used?



    Phil.

  • 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 :)

  • And how would the behaviour be, if I have a property like &quot;Inherits&quot; or &quot;Language&quot; or another reserved keyword?

  • 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.

  • &lt;%@ Page ShowHeaders=&quot;false&quot; %&gt;



    :)

  • 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.

  • 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.

  • 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

  • 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

  • Hi Matagatin,



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



    Thanks,



    Scott

  • 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?

  • 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

  • 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

  • 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?

  • Hi Dave,

    Unfortunately you can't modify the 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

  • 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.

  • 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

  • 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.

  • Hi Gabriel,

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

    Sorry!

    Scott

  • 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...

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

  • Hi Scott,
    is it also possible to create a new directive? for example?

  • 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

  • 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.

  • Is there a way to add this feature to an existing page with a code file? () 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.

  • 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

Comments have been disabled for this content.