A custom Page attribute, defined at top level

Last post 10-24-2005 6:24 PM by dabbi2005. 4 replies.

Sort Posts:

  • A custom Page attribute, defined at top level

    10-20-2005, 10:21 PM
    • Member
      405 point Member
    • dabbi2005
    • Member since 05-01-2005, 4:13 PM
    • Iceland
    • Posts 81

    Is there any custom attribute (property) I can set at the @Page declaration syntax for the Master page to read and parse?

    The Masterpage renders a particular image for every page but I want to be able to set a custom image or just disable it easily in some specific pages and so the Masterpage code-behind would read that particular attribute value and set the image properties accordingly...

  • Re: A custom Page attribute, defined at top level

    10-21-2005, 11:12 AM
    • Contributor
      4,347 point Contributor
    • dannychen
    • Member since 08-24-2004, 12:17 PM
    • Redmond, WA
    • Posts 840
    • AspNetTeam
      Moderator

    Yes.  If you have a baseclass that you inherit, you can put properties in there that can be specified on the directive.  This does not work for properties in the CodeBehind classes or declared on the page although the CodeBehind class can inherit from this baseclass.  This work on the same for Pages but I'll show MasterPages since you requested it.  Example:


    App_Code/BaseClass.vb:

    Public Class BaseClass
      Inherits MasterPage

      Public Property Test() As String
       ...
      End Property

    End Class

    Single File Master Page:
    <%@ Master Language="VB" Inherits="BaseClass" Test="This is some text" %>

    CodeBehind MasterPage:
    <%@ Master Language="VB" CodeFile="master.master.vb" CodeFileBaseClass="BaseClass" Inherits="Master" Test="This is some text" %>

    master.master.vb:

    Public Class Master
      Inherits BaseClass
    End Class

    Let me know if this is what you want!
    --
    Danny

    disclaimer: Information provided is 'as is' and conveys no warranties or guarantees.
  • Re: A custom Page attribute, defined at top level

    10-21-2005, 5:13 PM
    • Member
      405 point Member
    • dabbi2005
    • Member since 05-01-2005, 4:13 PM
    • Iceland
    • Posts 81

    thx very much altough I still haven't got it working. Actually I'm using C# and the attribute I want to add is to the Page control so I've tried:

    In App_Code/BaseClass.cs I put:

    public class BaseClass : System.Web.UI.Page
    {
       public string DecorationImage;
    }



    In Default.aspx codebehind I put:
    public partial class _Default : System.Web.UI.Page
    { ... }


    And so set for the same Default.aspx page:
    <%@ Page Language="C#" MasterPageFile="~/masterpage.master" CodeFile="Default.aspx.cs" Inherits="_Default" DecorationImage="roses" %>


    Well this stops me already at compilation level, VS says there is no such attribute defined!

  • Re: A custom Page attribute, defined at top level

    10-24-2005, 12:10 PM
    • Contributor
      4,347 point Contributor
    • dannychen
    • Member since 08-24-2004, 12:17 PM
    • Redmond, WA
    • Posts 840
    • AspNetTeam
      Moderator
     dabbi2005 wrote:



    In Default.aspx codebehind I put:
    public partial class _Default : System.Web.UI.Page
    { ... }


    And so set for the same Default.aspx page:
    <%@ Page Language="C#" MasterPageFile="~/masterpage.master" CodeFile="Default.aspx.cs" Inherits="_Default" DecorationImage="roses" %>


    Well this stops me already at compilation level, VS says there is no such attribute defined!



    This is where you strayed from my example. 

    First:  You need to inherits from the baseclass:

    public partial class _Default : BaseClass

    Second: You need to specify a CodeFileBaseClass attribute on your page directive:

    <%@ Page .... CodeFileBaseClass="BaseClass" %>

    --
    Danny
    disclaimer: Information provided is 'as is' and conveys no warranties or guarantees.
  • Re: A custom Page attribute, defined at top level

    10-24-2005, 6:24 PM
    • Member
      405 point Member
    • dabbi2005
    • Member since 05-01-2005, 4:13 PM
    • Iceland
    • Posts 81
    sorry was a little too fast to panic there, it's fixed and it's working. I actually found some code where ScottGu describes the whole process by implementing a custom Page class. It surprised me in the comment section so few saw the potentials of this... In my case it's a great feature I've been trying to code for weeks! I can now change the image source for a "decoration image" in the masterpage from just one single Page attribute instead of 2-3 lines of code within each and every Page_Load section of the content pages. I love it!!
Page 1 of 1 (5 items)