Navigation through Browser Redirection


Navigation consists of pointing the browser to a specific page in response to user interaction. The Hyperlink control, and various other complex controls that generate hyperlinks, cause the browser to load the specified page when the user clicks that link. However, you can force the browser to load a different page by sending back an "Object Moved" HTTP header that contains the new URL of the requested resource.

The simplest way to achieve this is through the Redirect method of the HttpResponse class. This method takes the URL of the page to load, and the browser will load that page instead of the current one. A limitation is that you must execute the Redirect method before you send any other output to the client. Once the headers are written and the main body of the page is being sent to the client, a call to the Redirect method will produce a runtime error.

A common approach is to clear the response buffer first, and then call the Redirect method. If you do not want your code to continue to execute after issuing the redirection to the client, you can call the End() method of the HttpResponse class.

Response.Clear(); Response.Redirect("http://www.mysite.com/newpage.aspx"); Response.End();


Alternatively, you can use the second overload of the Redirect method, and specify true for the second parameter to force ASP.NET to terminate execution.

Response.Clear(); Response.Redirect("http://www.mysite.com/newpage.aspx", true);


This technique is useful if you want to perform redirection only after executing some server-side code, perhaps to update a database or calculate the target URL dynamically based on some value provided by the user or the environment. For example, as shown in Listing 10.11, you might redirect the browser to a country-specific site using a value selected in a dropdown list on the page.

Listing 10.11. Using the Response.Redirect Method for Dynamic Redirection

void Page_Load() {   String newURL = String.Empty;   switch (MyDropDownList.SelectedValue)   {     case "us":       newURL = "http://mysite.com/";       break;     case "uk":       newURL = "http://mysite.com/uk/";       break;     case "de":       newURL = "http://mysite.de/";       break;     case default:       newURL = "http://mysite.com/countrylist.aspx";   }   Response.Redirect(newURL, true); }

The CommandName example, shown earlier in this chapter, is another prime candidate for the Response.Redirect method. While you can use a HyperlinkField in a GridView or other data display control to generate hyperlinks, you may need to interact with the value before performing the redirection. For example, if the value may be something that might be found on the Internet (perhaps it contains the word "web"), you could redirect to a search engine to get a listing of matching pages. Otherwise, you would display some data you had stored locally (see Listing 10.12).

Listing 10.12. Redirecting to a Search Engine with Response.Redirect

protected void GridView1_RowCommand(Object sender,                                     GridViewCommandEventArgs e) {   String details = e.CommandArgument.ToString();   if (details.IndexOf("web") > 0)   {     Response.Clear();     Response.Redirect("http://google.com/search?q=" + details, true);   }   else   {     ... just display local data ...   } }

You can also use the StatusCode, StatusDescription, and RedirectLocation properties of the Response class to cause a browser redirect, which is equivalent to using the Response.Redirect method. See http://msdn2.microsoft.com/en-us/library/y1hzy13b for more details.


Redirection with Client-Side Script

An alternative navigation approach is to perform client-side redirection using client-side script that runs in the browser. Extending the example shown in Figure 10.3 illustrates how it is easy to perform client-side redirection in response to any client-side event. All that is required is to set the window.location.href property in client-side script to the required URL, as shown in Listing 10.13, and the browser automatically loads from that URL. Just remember to return false from the client-side function to ensure that no postback occurs.

Listing 10.13. Client-Side Redirection with JavaScript Code

<script language="javascript" <!- function DoClientSideCode() {   window.location.href = 'thenewpage.aspx';   return false; } //-> </script> <asp:Button  runat="server" Text="Redirect"      OnClientClick="return DoClientSideCode()" />

Details of the client-side scripting object model are at http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp.




ASP. NET 2.0 Illustrated
ASP.NET 2.0 Illustrated
ISBN: 0321418344
EAN: 2147483647
Year: 2006
Pages: 147

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