RedirectA particularly useful method of the Response object is the Response.Redirect() method. This can be used to refer users to alternative Web pages. When they load a page that specifies a redirection, their browser loads the new page, which is specified in the Response.Redirect() method as follows : Response.Redirect( "http://www.UsingASP.net" ) Consider a multinational corporation that has servers in France, Australia, and the United States. The corporation runs a global Internet marketing campaign, where a single URL is plastered over hundreds of advertisements on sites all over the world. So when a potential customer clicks on an advertisement, you could send them to the nearest server, optimizing the use of resources both on the company servers and on the Internet as a whole. Note Users should be careful using the Response.Redirect() method. If the output is unbuffered and output has been previously sent to the browser, you can't redirect to a different page. You can see a demo program on www.UsingASP.net that offers users a selection of URLs to which they can go in a Select box. Users also can type a URL to which they want to go that's not in the list. The following code is a form that offers users the choices, and submits to RedirectResult.aspx when the Submit button is clicked: <form method="POST" action="RedirectResult.aspx"> Select a URL:<br> <select name="SelectURL"> <option value="" selected>No selection</option> <option value="http://www.disney.com">Disney</option> <option value="http://www.infinitevision.net">Infinite Vision Technologes</option> <option value="http://www.microsoft.com">Microsoft</option> <option value="http://www.devx.com">Devx</option> <option value="http://www.codeguru.com">Code Guru</option> </select><br> Or enter a URL: <br> <input type="text" name="URL" size="20"><br> <input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"> </form> Note This code is part of the page that you can find at www.UsingASP.net. (Once again, the formatting tags have been removed here in the text.) Select Chapter Examples, Chapter 4, then Redirect. You can see the page in Figure 4.4. Figure 4.4. This page offers a URL selection to users.
The receiving page first retrieves the URL text field. If the user has typed in a URL, the code doesn't bother to look in the SelectURL form field because it already has a URL to which it will redirect. If the user didn't enter a URL manually, a value is retrieved form the SelectURL field. If it's set to No Selection, the strURL string variable is emptied so that the redirect code knows not to perform the redirect. <% Dim strURL as String strURL = Request.Form( "URL" ).ToUpper() If strURL.Length = 0 Then strURL = Request.Form( "SelectURL" ) If strURL = "NO SELECTION" Then strURL = "" End If End If If strURL.Length > 0 Then Response.Redirect( strURL ) End If %> No URL was selected or typed in. Note This code is part of the page that is invoked by filling out the form shown in Figure 4.4 and clicking the Submit button. You can see the rendered page in Figure 4.5. Figure 4.5. No selection was made, so no redirection was performed.
|