What is the Page Directive?

S

senfo

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,
 
G

Guest

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
 
S

senfo

Peter said:
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(DesignerSerializationVisibility.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,
 
G

Guest

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 said:
Peter said:
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(DesignerSerializationVisibility.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,
 
S

senfo

Peter said:
<%@ 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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top