1. Tech

Your suggestion is on its way!

An email with a link to:

http://delphi.about.com/od/aspnet/l/aa073104a.htm

was emailed to:

Thanks for sharing About.com with others!

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).
Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
Join the Discussion
"Post your views and comments to this chapter of the free Asp.Net Delphi Programming Course"
Discuss!
Related Resources
A Beginner's Guide to Asp.Net Programming for Delphi developers.TOC
Elsewhere on the Web
About HTML
About JavaScript

Welcome to the tenth chapter of the FREE online programming course:
A Beginner's Guide to Asp.Net Programming for Delphi developers.
Exploring navigation techniques between Web Form pages: postbacks, direct navigation (using the <a> tag) and code-based navigation (using Server.Transfer and Response.Redirect).

To navigate or not to navigate
As 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.
Here's a simple scenario (taken from the BDSWebExample demo application): if a developer whishes to include its own examples in the listing, he/she first needs to log in - when the "Add Example" button is selected, if the user is not logged, he gets redirected to the Login.aspx page. After a successful login, the user is again redirected to the Manage.aspx page.

In this chapter, you'll learn about the following:

  • Navigating using "dummy" hyperlinks (<a> tags)
  • Smart <a> tag: HtmlAnchor control
  • On postbacks
  • Response.Redirect
  • Server.Transfer
Hyperlinks
The 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
Recall that we've already discussed
HTML Controls and their importance in ASP.NET development. 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 to. 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.

Here's an example (I'll assume you know how to server-side enable an HTML element):

TWebForm1 = class(System.Web.UI.Page)
...
strict protected
  smartLink : System.Web.UI.HtmlControls.HtmlAnchor;
...
  
procedure TWebForm1.Page_Load(
  sender: System.Object; 
  e: System.EventArgs);
begin
  //set the target page of the hyperlink
  smartLink.Href := 'Login.aspx';
  smartLink.InnerText := 'Login HERE!';
end;  

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):

  <a id=smartLink
     href=" " 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.

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.

Postbacks
One 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.Redirect
One 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:
procedure TWebForm1.Page_Load(
   sender: System.Object; 
   e: System.EventArgs);
begin
  Response.Redirect('WebForm2.aspx');
end;

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.Transfer
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.

One nasty side effect of using Server.Transfer, as in:

//suppose code-behind for FirstPage.aspx
procedure TFirstPage.Button1_Click(
    sender: System.Object; 
    e: System.EventArgs);
begin
  Server.Transfer('SecondPage.aspx');
end;

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:

  Response.Redirect(Request.Url.ToString);
To the next chapter: A Beginner's Guide to ASP.NET Programming for Delphi developers
That'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 >>
>> Web Forms navigation in ASP.NET - Part 2

©2014 About.com. All rights reserved.