What s New to the Page Framework?


What's New to the Page Framework?

Again, only some small changes were made for the Page Framework. Nevertheless, it's worth a closer look at the Framework surrounding the class Page class.

New Page Events

The Page class offers some new events that allow you even more interaction with the page's life cycle:

  • PreInit is raised before the initialization of the page. At this time, you may, for example, still define the desired Theme.

  • InitComplete is raised after the initialization is finished.

  • PreLoad is raised before the loading of the page.

  • LoadComplete follows the loading of the page.

  • PreRenderComplete is raised as soon as the PreRender event is completely handled. Here, for the last time you have the chance to make changes on the page before it's rendered.

Adding Headers

With the help of the new Header property of the Page class, you can make changes to the HTML head area in a very convenient way. The property holds an object that supports the IPageHeader interface. This way you can attach external style sheets, include style sheets, directly and define metadata. The latter happens as follows:

 <script runat="server"> void Page_Load(object sender, System.EventArgs e) {     this.Header.Metadata.Add("author", "Patrick A. Lorenz"); } </script> 

In the HTML source code delivered to the browser, it will look as follows:

 <html> <head>     <title>Untitled Page</title>     <meta name="author" content="Patrick A. Lorenz" /> </head> 

Setting Control Focus

The new possibility to set the cursor focus on an input control is very convenient. Validation controls make use of them as described previously. To do this, the abstract Control class implements a Focus method. Support for this method depends upon the control that is used. If you want to assign the focus to a text field, for example, you use the following command:

 this.Textbox1.Focus(); 

Alternatively, you can set the focus through the Page class. Here as well, a postback to the server will be done.

 this.SetFocus(this.Textbox1); 

You may additionally determine the initial focus by defining the defaultfocus attribute of the server-side HTML <form> tag:

 <form runat="server" defaultfocus="TextBox2"> 

Defining a Default Button

The missing possibility to set a standard button is a real deficit in previous versions of ASP.NET. In version 2.0, you may now very easily define if a button will be activated and then specify which button will be activated when a user presses the Enter key in a form.

The selection of the default button works in a similar way as setting the default focus through the defaultbutton attribute of the server-side HTML <form> tag:

 <%@ page language="C#" %> <script runat="server"> void Button1_Click(object sender, System.EventArgs e) {     this.LB_Message.Text = "You clicked button1"; } </script> <html> <head runat="server">     <title>Untitled Page</title> </head> <body>     <form runat="server" defaultbutton="Button1">         <asp:textbox  runat="server">         </asp:textbox>         <asp:button  runat="server"             text="Button" onclick="Button1_Click" />         <asp:label  runat="server"></asp:label>     </form> </body> </html> 

Note

This implementation of the default button is made in a way that could be refitted easily on the former ASP.NET versions. Unfortunately, form groups aren't considered, for example, in a similar way as validation groups. This feature will probably be reworked in the Beta version.

Accessing Validation Controls

While I'm covering validation groups, I should mention that the Page.Validators property returns a collection of all validation controls on page up to now. If you only need the controls assigned to a certain validation group, you should use the new method Page.GetValidators() instead and pass the name of the desired group.

The New Page.Items Dictionary

If you want to exchange data within a page, you can use the new dictionary offered by the Page.Items property. You can add any object to the dictionary during the life cycle of the page and access it anytime, for example, within user controls or custom controls.

One control that already uses this new possibility is WebPartManager, which I presented to you in Chapter 8. This control stores a reference for itself in the dictionary and is this way accessible for all the Web Parts on the page.

Caution

Please be aware that the content of the dictionary can't be kept persistent between postbacks, but it has to be created anew on every request (comparable to Context.Items).

Using Cross-Page Posting

You could have used the Server.Transfer method in previous versions of ASP.NET to switch from one page to another on the server side. The new version allows you to make a real cross-page posting so that you can post from one page to another within the current web site. This can be useful, for example, if you want to explicitly share a form or its processing on several pages.

If you want to post to another page, you must assign the desired URL to the PostTargetUrl property, which is presently being supported by the three controls: Button, LinkButton, and ImageButton. A small JavaScript function takes care that the target address of the form will be changed accordingly on the client. This functionality is used in Listing 11-15 by button1.

Listing 11-15: Posting the Form to a Different Page with the Button

start example
 <%@ page language="C#" %> <script runat="server"> void Button2_Click(object sender, System.EventArgs e) {     Label1.Text = "Hi " + TextBox1.Text + " you selected: " +         Calendar1.SelectedDate; } </script> <html> <body>     <form  runat="server">         Enter Name:         <asp:textbox  runat="server"></asp:textbox>         <asp:button  runat="server"             text="Postback to Another Page"             posttargeturl="~/crosspost2.aspx" />         <br /><br />         Pick Date:         <br /><br />         <asp:calendar  runat="server">         </asp:calendar>         <br /><br />         <asp:button  runat="server"             text="Postback to Same Page"             onclick="Button2_Click" />         <br /><br />         <asp:label  runat="server" font-size="Large">         </asp:label>         <br /><br />     </form> </body> </html> 
end example

The button refers to a second page with the filename crosspost2.aspx. Inside this second page, the previous page is accessible through the PreviousPage property. You can, for example, access the contained controls with the help of the FindControl() method and reuse their content in the second page. As shown in Figure 11-14 and Listing 11-16, this is used to display the entered name and the selected date of the first page.

Listing 11-16: Accessing the Previous Page with the Cross-Post Destination Page

start example
 <%@ page language="C#" %> <script runat="server"> void Page_Load(object sender, System.EventArgs e) {     TextBox textBox1 = (TextBox) PreviousPage.FindControl("TextBox1");     Calendar calendar1 = (Calendar) PreviousPage.FindControl("Calendar1");     this.Label1.Text = string.Format("Hi {0} you selected {1}",         textBox1.Text, calendar1.SelectedDate); } </script> <html> <body>     <form  runat="server">         <asp:label  runat="server" font-size="X-Large"></asp:label>     </form> </body> </html> 
end example

click to expand
Figure 11-14: You can now post to different target pages.

As long as you already know the original page on the target page, you can reference it through the use of the new directive @PreviousPage. You can either enter the page's virtual path or its type name. This way, you get type-safe access to the previous page so that you can exchange data by using properties:

 <%@ page language="C#" %> <script runat="server"> public string Text {     get { return this.TextBox1.Text; } } public DateTime SelectedDate {     get { return this.Calendar1.SelectedDate; } } </script> 

You can access the properties on the target page directly through the PreviousPage property:

 <%@ page language="C#" %> <%@ previouspage virtualpath="crosspost3.aspx" %> <script runat="server"> void Page_Load(object sender, System.EventArgs e) {     if (this.IsCrossPagePostBack)     {         this.Label1.Text = string.Format("Hi {0} you selected {1}",             this.PreviousPage.Text, this.PreviousPage.SelectedDate);     } } </script> 

As you can see from the previous listing, you can use the IsCrossPagePostBack property to find out if the page has been accessed using a cross-page postback or not. Outside of the Page class, by the way, you get access to the current or previous pages through the Context.CurrentHandler and the Context.PreviousHandler properties.

Caution

Please be aware that at present cross-page posting is only possible between two pages of the same web site. You can't define external addresses. This limitation will presumably be gone in the Beta version.

Setting Properties in the @Page Directive

With the new version of ASP.NET, the @Page directive got a reasonable enhancement. You can not only assign its attributes, but also any properties of the Page class or of an inherited class.

You can use the new capability, for example, to assign an individual title to a page. I've already presented some possible solutions for this frequently asked feature in Chapters 4 and 5. The following example goes one step further and allows you to set the title through the @Page directive.

The example in Listing 11-17 consists of three files:

  • The TitledPage base class, which derives from Page and implements a single property, Title.

  • The masterpage5.aspx Master Page, which requests the chosen title through the TitledPage class and assigns it to a Literal control.

  • The contentpage5.aspx Content Page, which allows the assignment of an individual title without any source code just by using the @Page directive.

Listing 11-17: @Page allows setting of any page properties.

start example
 // Code/ TitledPage.cs using System; public class TitledPage : System.Web.UI.Page {     private string title;     public string Title     {        get { return this.title; }        set { this.title = value; }     } } // masterpage5.master <%@ master language="C#" %> <script runat="server"> void Page_PreRender(object sender, System.EventArgs e) {     this.LT_HtmlTitle.Text = ((TitledPage)this.Page).Title; } </script> <html> <head runat="server">     <title><ASP:Literal  runat="server" /></title> </head> <body>     <form runat="server">         <asp:contentplaceholder  runat="server">         </asp:contentplaceholder>     </form> </body> </html> // contentpage5.aspx <%@ page language="C#"     master="~/MasterPage5.master"     inherits="TitledPage"     Title="Hello World!" %> <script runat="server" language="c#"> </script> <asp:content      contentplaceholder     runat="server">     Just a simple content page </asp:content> 
end example

The title was used properly, as you can see in Figure 11-15. If you want to use this approach for your own web site, you can assign the new page TitledPage base class for the whole site through the configuration file web.config. This way, you don't have to assign it on each single page.

click to expand
Figure 11-15: The page's title is set using the @Page directive.

Tip

Setting properties works not only with the @Page directive, but also with the @Control directive and even the new @Master directive in Master Pages.

Adding Default Namespaces

As in the previous versions of ASP.NET, a couple of frequently used namespaces are imported on all pages by default. This way, you can use classes from, for example, System.Web, System.UI, System.Web.UI.WebControls, and so on directly. Importing additional namespaces is possible as before through the @Import directive.

Additionally, the new version gives you the chance to enlarge the listing of default namespaces. Just add the desired namespaces to the corresponding section in the configuration file, web.config, as follows. The import will be considered during the compilation, and it's well supported by IntelliSense in the VS .NET development environment.

 <?xml version="1.0"?> <configuration>     <system.web>         <pages>             <namespaces>                 <add namespace="System.IO" />             </namespaces>         </pages>     </system.web> </configuration> 




ASP. NET 2.0 Revealed
ASP.NET 2.0 Revealed
ISBN: 1590593375
EAN: 2147483647
Year: 2005
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net