16.5 Redirecting Users to a Different Web Page

 <  Day Day Up  >  

You want to redirect users to a different Web page or Web site using server-side code.


Technique

The class that is created for each ASP.NET page is derived from the System.Web.UI.Page class within the .NET Framework. Within this class is a Response object that is used to send data constituting a response from the client's initial request. To cause a page redirection to occur, call the Response.Redirect method, passing the URL of the page to redirect the user to. You can optionally pass a Boolean value for the second parameter. If this value is true , then execution of the current page stops. In the following example, an event handler for a Button control redirects a user to a different site:

 
 private void btnGo_Click(object sender, System.EventArgs e) {     Response.Redirect( "http://www.samspublishing.com" ); } 

Comments

The Response object is created as a result of an initial request by a remote user for a Web page. The request itself is packaged and placed within a HttpRequest object. The page being requested is loaded by ASP.NET, which subsequently creates a Page object, and a response is formulated to send back to the client by creating a HttpResponse object. Both the HttpRequest and the HttpResponse are managed by the Page object, which means that because your class derives from the Page class, you have full access to those objects via the Request and Response properties.

The HttpResponse class contains several properties and methods that allow you to alter or add data to the outgoing response stream sent back to a client. For instance, if you want to create a custom header value, call the AddHeader method passing a key/value pair. It is then added to the header collection sent to the client. ASP also used the Response object extensively to add HTML data to the output stream by calling the Response.Write method. The same applies to ASP.NET, although the line becomes a little fuzzy about where to call Response.Write because of the separation of presentation and logic using code-behind files. For more information on writing to the output stream, see Recipe 16.14, "Using Page Output Caching."

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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