Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Implement an ASP.NET Back Control
Give Web pages a server-side control that redirects the user's browser to the referring page.
by Juval Löwy

July 2003 Issue

Technology Toolbox: C#, ASP.NET

Q: Implement an ASP.NET Back Control
I want to add a link to my ASP.NET pages that goes back to the page that referred them. How do I use a server-side control to do this? I need to have control over the referred page, and using a browser history isn't an option.

A:
You have two ways to implement a "back" link on a Web page. The first is to use a client-side script to access the browser history of visited pages and insert a redirection to the previous page:

<a href=
   "javascript:history.back()"
         >Back</a>

ADVERTISEMENT

However, this technique has several disadvantages. The application has no control over where the user is redirected. You often want to keep the users inside the application, and you don't want them to wander off to other pages. This solution works only if the browser supports client-side script. Its biggest disadvantage is that it's inconsistent with the ASP.NET programming model. One of ASP.NET's greatest benefits is that—unlike classic ASP—it doesn't require you to rely on client-side script. You simply use server-side controls that you write in managed code, and ASP.NET does the rest. When you use client-side script, you disconnect the back link from the rest of your application, which executes on the server and uses server-side controls. This leaves you no easy way to enable or disable the back link or control the redirection, based on server-side event processing.

The second solution is to use an ASP.NET custom user control. The source files accompanying this article contain the BackLink ASP.NET user control in the WebControlsEx class-library assembly. To use it, right-click on the Web Forms toolbox, and select Add/Remove Items… from the popup context menu. Click on the Browse… button on the .NET Frameworks Components tab. Select the WebControlsEx assembly and click on Open. This adds the BackLink user control to the toolbox. You can simply drag and drop it onto your forms to add a link-button-like control to the form, with the text set to "Back." The control presents the same design-time properties as the standard link button: You can change the text, font size, color, boldness, annotation, and other properties, such as link style (see Figure 1). The back-link control redirects the browser to the previous page, if one is available, when the user clicks on the button at run time.

Implementing the BackLink user control is more challenging than meets the eye. You must start with a DLL class library in order to build a user Web control. You can provide a user control either by deriving from a class called WebControl and doing the rendering yourself, or by deriving from an existing control and specializing its behavior. Deriving from a LinkButton is the better option by far for a back-link control, because the LinkButton control provides most of the difficult-to-implement functionality.

1 2 3 Next
Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home