In a world of desktop applications, where you build programs with multiple forms, you almost always end up calling one form from another and passing values from one form to another.
In ASP.NET, navigation between web form pages is the process of redirecting users from one web forms page to another or requesting updates for the current page.
In this blog, you'll learn about the following:
- Navigating using "dummy" hyperlinks (<a> tags)
- Smart <a> tag: HtmlAnchor control
- On postbacks
The easiest way to redirect to another page is to add a tag as a HTML element to the page. When using hyperlinks the target page is "hard-coded" in the "href" attribute of the <a> element.
Here's an example:
Please <a href="Login.aspx">Login HERE</a>
Smart tag: HtmlAnchor control
The HtmlAnchor class allows programmatic access to the HTML <a> tag on the server. When using HtmlAnchor, the target page can be set using the HRef property to define the location of the page to link. Server-side enabled version of the <a> tag gives us an easy option to "loose-code" the target page - since you can alter the HRef tag before the page gets rendered to the user.
//set the target page of the hyperlink
smartLink.Href := 'Login.aspx';
smartLink.InnerText := 'Login HERE!';
This is how the HTMLAnchor control looks at design time (after you drop it on a form and set the ID and the runat=server attributes):
<aid=smartLinkhref=" " runat="server">Anchor</a>
And finally, when you run the application, here's how it gets rendered:
<a href="Login.aspx" id="smartLink">Login HERE!</a>
When using hyperlinks to redirect a user to another page, you are using direct page navigation. Direct navigation refers to the use of standard HTML/DHTML elements and techniques.
A "postback" is simply a special case of direct navigation. The target Web Form is the same Web Form that is currently being viewed. In ASP.NET, the most common cause of a postback is a click of a button (Web or Html control). Another cause of a postback could be, for example, changing the value of a TextBox (when the AutoPostBack property for the TextBox control is set to True). Handling postbacks is very important in ASP.NET, it enables you to catch and process events on the server-side.
We'll now take a closer look at two techniques for code-based navigation. Code-based means that redirecting to another Web Form is managed by server-side code. Note that HtmlAnchor provides a very simple means of code-based navigation.
One of the common ways to redirect a user to another page using server-side code is using the response property of the Web Form. The response property gets the HttpResponse object associated with the page. The HTTPResponse object allows you to send HTTP response data to a client. The redirect method of the HTTPResponse class redirects a client to a new URL.
Here's an example of Response.Redirect:
The bad thing with using Response.Redirect is that it requires a roundtrip back to the client to perform the navigation. On a first request, the page thatcontains the redirection is processed (WebForm1); on a second request, the redirected page is obtained (WebForm2.aspx).
The server property of the Web Form gets the server object, which is an instance of the HttpServerUtility class. When you call the transfer method, ASP.NET terminates processing of the current request (that is: stops processing the code; as if you called "GoTo") and simply transfers the user to another page.
//suppose code-behind for FirstPage.aspx
One side effect of using Server.Transferis that the page URL displayed in the browser would still be "FirstPage.aspx", even though the user is actually looking at the SecondPage.aspx Web Form.
Note that using Server.Transfer does not require an extra roundtrip.
Another interesting result of using Server.Transfer is that the calling page gets executed entirety. This means that if you call Server.Transfer('FirstPage.aspx'); from the code-behind file for FirstPage.aspx - the IsPostback property will be set to False - and all the initialization code for the Web Form will be executed again.
Another approach to redirecting to the current page without "raising" a postback is to use the Url (an instance of the System.Url class) property of the HTTPRequest object. The URL property gets information about the URL of the current request. A call like: