By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,561 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

What is the Page Directive?

P: n/a
This is probably a really stupid question, but what exactly is the <%@
Page %directive? I thought I knew the answer to this, but now I'm not
so sure that I do.

Essentially, what I'd hope to accomplish, is to be able to add new
attributes to the Page element to provide custom functionality. For
example, one of the ASP.NET 2.0 apps that I'm working on has a help
button on most pages. The help button, which is part of the MasterPage,
should automatically provide a link to a help page, which is relevant to
the page the user is on. If it's possible, I'd like to be able to set
the location of the help file in an attribute in the markup like the
following:

<%@ Page language="c#" MasterPage="MasterPage.Master" Inherits="Foo"
CodeFile="Foo.aspx.cs" HelpFile="Help/Foo.html" %>

I thought it only be a matter of creating a new class that inherited
from the Page class, and then adding a HelpFile property. Naturally,
I'd have to update the code behind for my web forms to inherit from my
CustomPage class, rather than System.Web.UI.Page. Unfortunately, this
did not work. Doing so resulted in a compiler error, indicating that
the HelpFile attribute was not valid for the Page element.

Is there any way I can add new attributes to the Page element?

Thank you in advance,

--
Sean

website: http://senfo.blogspot.com
Apr 4 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
The @Page directive is the infrastructure to "hook up" the ASPX portion of an
ASP.NET web page to the Page (or Page-derived) class that represents the
server-side processing of this ASPX and all its tags and properties.

You might start out by simply having your pages derive from a custom base
class (which itself derived from Page class) and put your custom "stuff" in
that.

Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"senfo" wrote:
This is probably a really stupid question, but what exactly is the <%@
Page %directive? I thought I knew the answer to this, but now I'm not
so sure that I do.

Essentially, what I'd hope to accomplish, is to be able to add new
attributes to the Page element to provide custom functionality. For
example, one of the ASP.NET 2.0 apps that I'm working on has a help
button on most pages. The help button, which is part of the MasterPage,
should automatically provide a link to a help page, which is relevant to
the page the user is on. If it's possible, I'd like to be able to set
the location of the help file in an attribute in the markup like the
following:

<%@ Page language="c#" MasterPage="MasterPage.Master" Inherits="Foo"
CodeFile="Foo.aspx.cs" HelpFile="Help/Foo.html" %>

I thought it only be a matter of creating a new class that inherited
from the Page class, and then adding a HelpFile property. Naturally,
I'd have to update the code behind for my web forms to inherit from my
CustomPage class, rather than System.Web.UI.Page. Unfortunately, this
did not work. Doing so resulted in a compiler error, indicating that
the HelpFile attribute was not valid for the Page element.

Is there any way I can add new attributes to the Page element?

Thank you in advance,

--
Sean

website: http://senfo.blogspot.com
Apr 4 '07 #2

P: n/a
Peter Bromberg [C# MVP] wrote:
The @Page directive is the infrastructure to "hook up" the ASPX portion of an
ASP.NET web page to the Page (or Page-derived) class that represents the
server-side processing of this ASPX and all its tags and properties.

You might start out by simply having your pages derive from a custom base
class (which itself derived from Page class) and put your custom "stuff" in
that.
Hi Peter,

First of all, thank you for the reply.

I guess I wasn't clear. I actually have done this, already (assuming I
understood you correctly). My CustomPage class looks like the following
(manually typed, please ignore any typos):

public class CustomPage : System.Web.UI.Page
{
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializat ionVisibility.Hidden),
EditorBrowsable(EditorBrowsableState.Never)]
public string HelpFile
{
get { [...] }
set { [...] }
}

public CustomPage()
{
}
}

Having created that class, I updated the code behind for my page to
inherit from that base class, rather than System.Web.UI.Page, as in the
following:

public partial class Foo : CustomPage
{
[...]
}

Lastly, I added a HelpFile attribute to the Page directive in my markup:

<%@ Page language="c#" MasterPage="MasterPage.Master" Inherits="Foo"
CodeFile="Foo.aspx.cs" HelpFile="Help/Foo.html" %>
When I attempt to compile that, a compiler error is generated,
acknowledging that the HelpFile attribute is not valid for the Page element.

Am I missing something else?

Thank you again,

--
Sean

website: http://senfo.blogspot.com
Apr 4 '07 #3

P: n/a
If you want to add a custom attribute to the @Page directive (such as
HelpFile, which I am not sure is actually available for use) you can use code
like the following (this sample actually comes from Scott Guthrie's blog):

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>

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"senfo" wrote:
Peter Bromberg [C# MVP] wrote:
The @Page directive is the infrastructure to "hook up" the ASPX portion of an
ASP.NET web page to the Page (or Page-derived) class that represents the
server-side processing of this ASPX and all its tags and properties.

You might start out by simply having your pages derive from a custom base
class (which itself derived from Page class) and put your custom "stuff" in
that.

Hi Peter,

First of all, thank you for the reply.

I guess I wasn't clear. I actually have done this, already (assuming I
understood you correctly). My CustomPage class looks like the following
(manually typed, please ignore any typos):

public class CustomPage : System.Web.UI.Page
{
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializat ionVisibility.Hidden),
EditorBrowsable(EditorBrowsableState.Never)]
public string HelpFile
{
get { [...] }
set { [...] }
}

public CustomPage()
{
}
}

Having created that class, I updated the code behind for my page to
inherit from that base class, rather than System.Web.UI.Page, as in the
following:

public partial class Foo : CustomPage
{
[...]
}

Lastly, I added a HelpFile attribute to the Page directive in my markup:

<%@ Page language="c#" MasterPage="MasterPage.Master" Inherits="Foo"
CodeFile="Foo.aspx.cs" HelpFile="Help/Foo.html" %>
When I attempt to compile that, a compiler error is generated,
acknowledging that the HelpFile attribute is not valid for the Page element.

Am I missing something else?

Thank you again,

--
Sean

website: http://senfo.blogspot.com
Apr 4 '07 #4

P: n/a
Peter Bromberg [C# MVP] wrote:
<%@ Page Language="C#" message="My Test Message String" Inherits="BasePage"
%>
Hi Peter,

I think I figured it out. As far as I can tell, Scott Guthrie was
missing the CodeFileBaseClass attribute. Without that, it appeared as
though the page side of the partial class was implementing the Page
class, even though the code behind side of the partial class was
implementing my CustomPage class. So the Page directive should look
like the following:

<@ Page Language="C#" Message="Testing 123" Inherits="test"
CodeFileBaseClass="BasePage" CodeFile="test.aspx.cs" %>

Thank you again,

--
Sean

website: http://senfo.blogspot.com
Apr 5 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.