Controlling Page Navigation


In the following sections, you learn how to control how a user moves from one ASP.NET page to another. First, you learn how to submit an HTML form to another page and retrieve form information. Next, you learn how to use the Redirect() method to automatically transfer a user to a new page. Finally, you learn how to link pages together with the HyperLink control.

Submitting a Form to a Different Page

You might not have noticed, but all the ASP.NET pages discussed up to this point in the book have posted back to themselves . There is a good reason for this. Most of the ASP.NET controls take advantage of a page's view state to retain information between posts. If you use the server-side version of the <form> tag, you cannot post a form to a different page (the server-side form tag does not have an Action property).

You can, if you have a pressing need, post a form to a new page by using the standard HTML form tags instead of ASP.NET controls as illustrated in the page contained in Listing 2.48.

Listing 2.48 Form.aspx
 <html> <head><title>Form.aspx</title></head> <body> <form Method="Post" Action="Results.aspx"> Username: <br> <input name="username"> <p> Comments: <br> <TextArea name="comments"   cols="50" rows="10"></textarea> <p> <input type="submit"> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

In Listing 2.48, the Action attribute of the <form> tag is assigned the name of a new page, Results.aspx , where the form information is posted.

Because the form information is posted to a different page, you cannot retrieve that information by using ASP.NET controls. For example, you cannot refer to the Username control within the Results.aspx page. Instead, you need to use the Params collection of the HTTPRequest class.

NOTE

You also can use the Form collection of the HTTPRequest class to grab form data. The difference between the Params and Form collections is that the Params collection also represents QueryStrings , ServerVariables , and Cookies .


The Results.aspx page in Listing 2.49 demonstrates how you can use the Params collection to retrieve values of the Username and Comments form fields.

Listing 2.49 Results.aspx
 <Script Runat="Server"> Sub Page_Load   lblUsername.Text = Request.Params( "Username" )   lblComments.Text = Request.Params( "Comments" ) End Sub </Script> <html> <head><title>Results.aspx</title></head> <body> <asp:Label   ID="lblUsername"   Runat="Server" /> wrote <asp:Label   ID="lblComments"   Runat="Server" /> </body> </html> 

The C# version of this code can be found on the CD-ROM.

Using the Redirect() Method

Normally, you do not want to post form data to a separate page. In most cases, you should take advantage of view state and have the form post back to itself. So, the question remains, how do you transfer a user to a new page after the user has successfully completed a form?

The best way to transfer the user is to use the Response.Redirect() method. You can use this method to automatically transfer a user to any page on your Web site. You can even use it to transfer a user to a page on another Web site (such as the home page of Yahoo.com).

The page in Listing 2.50 demonstrates how you use the Response.Redirect() method with a form. After the form is submitted, the Redirect() method transfers the user to a page named ThankYou.aspx . Typically, you would save the form data to a file or database table right before the Redirect() method is called.

Listing 2.50 Redirect.aspx
 <Script Runat="Server"> Sub Button_Click( s As Object, e As EventArgs )   ' Save Form Data to Database Table   Response.Redirect( "ThankYou.aspx" ) End Sub </Script> <html> <head><title>Redirect.aspx</title></head> <body> <form Runat="Server"> Username: <br> <asp:TextBox   ID="txtUsername"   Runat="Server" /> <p> Comments: <br> <asp:TextBox   ID="txtComments"   TextMode="MultiLine"   Runat="Server" /> <p> <asp:Button   Text="Submit!"   OnClick="Button_Click"   Runat="Server" /> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

Working with the HyperLink Control

You can link one ASP.NET page to another by adding a HyperLink control to a page. It can display either text or an image as a link. All the properties, methods , and events of this control are listed in Table 2.12.

Table 2.12. HyperLink Properties, Methods, and Events

Properties

Description

ImageUrl

The URL of an image to display for the link.

NavigateUrl

The URL of the page to which the user is transferred when the link is clicked.

Target

The target window or frame for the link. Possible values are _top , _self , _parent , _search , or _blank .

Text

The text label for the link.

Methods

Description

None

 

Events

Description

None

 

The advantage of using a HyperLink control rather than a simple HTML link is that you can manipulate its properties in your code. For example, the HyperLink control in Listing 2.51 links to different pages depending on the time of day. After 5:00 p.m., the HyperLink control links to a page named AfterHoursHelp.aspx and before 5:00 p.m., it links to Help.aspx .

Listing 2.51 HyperLink.aspx
 <Script Runat="Server"> Sub Page_Load   If TimeOfDay > #5:00pm# Then     lnkHelpLink.NavigateUrl = "AfterHoursHelp.aspx"     lnkHelpLink.Text = "After Hours Help"   Else     lnkHelpLink.NavigateURL = "Normalhelp.aspx"     lnkHelpLink.Text = "Help"   End If End Sub </Script> <html> <head><title>HyperLink.aspx</title></head> <body> <form Runat="Server"> Click on the link below to receive a list of contact phone numbers for help information: <p> <asp:HyperLink   id="lnkHelpLink"   Runat="Server" /> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

Similar book on Amazon

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