| Web Forms navigation in ASP.NET - Part 1 | ||||||||||||||||||||||||||||
| Exploring navigation techniques between Web Form pages: postbacks, direct navigation (using the <a> tag) and code-based navigation (using Server.Transfer and Response.Redirect). | ||||||||||||||||||||||||||||
Welcome to the tenth chapter of the FREE online programming course: To navigate or not to navigateAs soon as you start developing more complex ASP.NET applications using Delphi (read: web applications with more than one Web Form / aspx page), you'll need to be able to redirect users from one Web Form page to another page.This sounds pretty familiar: in a world of desktop applications, where you build programs with multiple forms, you almost always end up with calling one Form from another (be it modal or not) 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 chapter, you'll learn about the following:
HyperlinksThe easiest way to redirect to another page, is to add an tag as an HTML element to the page. Adding an <a> tag is not ASP.NET specific kind of navigation - hyperlinks are in the heart and soul of every web site. 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 Here's an example (I'll assume you know how to server-side enable an HTML element):
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):
And finally, when you run the application, here's how it gets rendered:
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. Another interesting way to redirect a user to another page is using client-side (JavaScript) code and DHTML. For example, the window.showModalDialog creates a modal dialog box that displays the specified HTML document. In general, a call to client-side function that executes window.showModalDialog is usually attached to an event on a control or hyperlink. Note: the Mono Web Dialogs package uses such navigational techniques to construct the "output" for the MessageDlg or the InputBox / InputQuery function. I'm not providing any examples for client-side navigation, since this would require the knowledge of JavaScript, DOM model, DHTML, etc. Of course, the first time we discuss client-side programming in ASP.NET I'll have a few nifty examples. PostbacksOne could say that postback handling of Web Forms is what ASP.NET as all about. We've already started discussing postback processing and what a postback in ASP.NET is, in some of the previous chapters. 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 to 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. An example of using the IsPostback Web Form property was explained in an earlier chapter.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. Response.RedirectOne of the common ways (in classic ASP) 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 contained the redirection is processed (WebForm1); on a second request, the redirected page is obtained (WebForm2.aspx). On the other hand, if you are using Response.Redirect to take a user to a completely new web site (to an advertiser on your site) you might just want to process those two trips. First you increase the advert counter (to be able to track banner clicks, for example), than you redirect the user to a correct location. Server.TransferThe 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.One nasty side effect of using Server.Transfer, as in:
is 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 in its 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 "rasing" a postback is to use the Url (an instance of the System.Uri class) property of the HTTPRequest object. The URL property gets information about the URL of the current request. A call like:
To the next chapter: A Beginner's Guide to ASP.NET Programming for Delphi developersThat's it for today, in the next chapter we'll continue to explore Web Forms navigation an passing values from one Web form to another and back. Stay tuned!If you need any kind of help at this point, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts. A Beginner's Guide to ASP.NET Programming for Delphi developers: Next Chapter >> |
||||||||||||||||||||||||||||

