Navigating Conditionally

See Also

In static Web pages, you link pages together using the <A> tag, as in the following example:

Click <A HREF="home.asp">here</A> to return to the home page.

However, by using scripts, you can navigate through the application dynamically, specifying target pages based on conditions in the application. For example, a page in your application might ask the user for a password. If the user provides a correct password, the application displays a welcome page, otherwise it displays an error message.

Note   You can use the scripting object model and page objects to design navigation for your Web pages. For details, see Extending the Scripting Object Model Across Pages.

Navigating from Client Script

If you are writing client script, you can control the page that the browser displays next by calling a method or setting a property.

To navigate to another page in client script

For example, the following client script reads the value of a check box in a form, and depending on the user's preference, navigates to one of two possible pages:

<SCRIPT LANGUAGE="VBScript">
Sub NextPage()
   ' The following reads the value of a checkbox called Checkbox1
   fReviewIntro = document.frmNavBar.Checkbox1.checked
   If fReviewIntro then
      window.navigate("http://MyServer/intro.htm")
   Else
      window.navigate("http://MyServer/page2.htm")
   End If
End Sub
</SCRIPT>

The following is a similar example, but sets the window object's href property to navigate:

   If fReviewIntro Then
      window.location.href = "http://MyServer/intro.htm"
   End If

For more information about using the window object, see DHTML Objects.

Navigating from Server Script

You can also control the next page displayed in a browser from server script. You use server script to navigate if the condition for moving to a specific page depends on information only available to the server — for example, information from a database, from a Session or Application object variable, and so on.

To navigate in server script

For example, you might want to make sure users log in to your application before viewing pages. If they attempt to navigate to a page deeper in your application without logging in first, you want to detect that and send them to the login page instead. In the following example, a Session object variable contains a flag indicating whether the user has already logged on:

<%
If Not Session("Been_to_Home_Page") Then 
   Response.Redirect "homepage.asp" 
End If
%> 

Buffering Response

By default, when the server sends a page to the browser, it streams the output as soon as it is composed. However, you can have the server buffer the output.If you do, the page is completely processed at the server before being sent. The advantage is that you can then call a method such as Response.Redirect to send a different page instead. If the page is not buffered, the server reports an error if you try to redirect a page after some of the current page has already been sent.

To set buffering


Managing Sequential Page Navigation

If you are navigating in server code, you can use a bundled component that can manage sequential navigation through a list of pages. Rather than maintaining URL references in a number of ASP pages, you can specify the sequential organization of pages in a single, easy-to-edit text file.

To create sequential page navigation

  1. Create a text file that contains a list of pages, one per line.

  2. Create a NextLink object.

  3. Use the NextLink object's methods to move between pages in the list.

The following example reads the link order from a text file and creates a table of contents on a single page.

<% Set NextLink=Server.CreateObject("MSWC.NextLink") %>
<% count=NextLink.GetListCount("/Nextlink.txt") %>
<% i=1 %>
<UL>
<% For i = 1 to count %>
    <LI><A HREF="<%= NextLink.GetNthUrl("/nextlink.txt",i) %>">
    <%= NextLink.GetNthDescription("/nextlink.txt",i) %></A>
<% Next %>