Download this article to see resources
This article tells how to add a server-side control to the web page to redirect the user's browser to the page pointed to by the control.
By JuVal Lowy
Technology Toolbox: C #, ASP.NET
Q: Implement an ASP.NET Back control I want to add a link to the ASP.NET page, through it, I can return to the page it points to. I don't know how to use a server-side control to implement it? I want to return to the visited page through this control, and I don't want to browse the history of history.
A: You can use two ways to implement a "back" link on a web page. The first method is to read the record of the previously accessed page with the client script and redirect it to the previous page:
"JavaScript: History.back ()> Back
However, this approach has several disadvantages. The application cannot control where the user is redirected. Typically, you always want users to stay in the app without wanting them to run to other pages. This method is only possible when the browser supports the client script. The biggest disadvantage is that it is inconsistent with the ASP.NET programming mode. One of the biggest benefits of ASP.NET is that it is different from traditional ASP, which does not require you to rely on the client script. You only need to know how to use the server-side control written by the hosting code, and ASP.NET will do other work for you. When using the client script, the Back link is not related to the rest of the application, which is running on the server and uses the server side control. This will make it difficult to control whether to activate your back link, and you are also difficult to control the redirect because they are based on the server-side event handler.
The second method is to use an ASP.NET custom user control. The source files included herein include a Backlink ASNET user control in the WebControlsex class library program. If you want to use this control, you need to right-click on the Web Forms Toolbox, select Add / Remove Items from the pop-up context menu. Click the .NET Frameworks Components Tab button and press the Browse button. Select the WebControlsex assembly, point Open. This adds a Backlink user control on Toolbox. You only need to drag and drop it into your Form, you can add a link-type control, and its text property is "back". In design, the control has the same properties as the standard link button, such as a link style (see Figure 1). When the user clicks the button at runtime, it boots the browser to the previous page (if any).
Implementing the Backlink user control is not as simple as we see. To build a web user control, you must use a DLL class library. You can provide a user control by two ways: inheriting a class called WebControl and customizes it, or inherits an existing control and customizes. For Backlink controls, a better way is to inherit a linkbutton because the LinkButton control provides most difficult features.
You can inherit a class called Backlink from LinkButton, set the Text property to Back in its constructor, and provide some meaningful defaults:
Public Class Backlink: LinkButton
{
Public backlink ()
{
Text = "back";
}
}
In fact, inheriting a LinkButton can be reflected in HTML. By inheriting Backlink, the control can reflect all properties of LinkButton in the designer - such as default properties, font setting properties, and events. LinkButton has a protected virtual method called OnClick (). When the page is returned, .NET will call this method, let the link button trigger a click event. LinkButton must overclick () and redirect the user to the page pointed to by the control.
Important issues are on the BACKLINK control range, how to get the page pointed to by the server side. Fortunately, ASP.NET has such a function: The HTTPRequest object provides an open property of a URL type, called URLREFERRER, which represents the URL of the point to the page. The LinkButton user control can get a reference to the page, which is implemented by the Control class. Control is the base class of WebControl, WebControl is the base class of LinkButton. This control can also be used to read its HttpResponse object (Response property) with the PAGE property to redirect the user to the pointing page.
However, if you use the following code to implement the Backlink control, the link will not work:
Public Class Backlink: LinkButton
{
Public backlink ()
{
Text = "back";
}
Protected Override Void
Onclick (Eventargs E)
{
URI backURL =
Page.Request.urlReferRer;
Page.Response.Redirect
Backurl.absoluteuri;
}
}
The reason is how the ASP.NET traces the way you point to the page. For example, we take Home.aspx pages as an example, which boots users to the Subform.aspx page. If the Subform.aspx page has a Backlink control, the user will trigger an event that returns a page to the server after the user clicks the link. The value of the URLREFERRER attribute on the server is "subsform.aspx" (instead of "home.aspx"), because when returns a page, Subform.aspx points to itself. The solution is to cache the pointing page in the session variable when loading the control, and reforms the point to the point to the point in OnClick ().
Now you need to solve a few questions: ASP.NET does not always provide pages with guiding information, in which case ASP.NET sets the value of UrlReferRer empty. You also need to provide a "smart" Back link that redirects to the logical front page. In other words, if a page containing the backlink is overloaded (after processing some of the controls), the Back link should be "smart" to detect this and redirect to the front page of "real". Instead of it itself. Note that if you use the client code, this is impossible because it is loaded again, the BACK record variable presents the same page.
Backlink covers the ONLOAD () method of its base class (see List 1). When loading a page containing the Backlink control, OnLoad () is called. OnLoad () first checks if there is point to page information. If URLREFERRER is empty, ONLOAD () will not work back link. If you have oriented information, you need to verify that the page pointed to which is not the current page. You can implement this by comparing the URL of the Page and the URL of the Request page: IF (Backurl.absolutePath! =
Page.Request.url.absolutePath)
If the two URLs are different, Backlink saves the URL of the page to a session variable:
Page.Session ["Referring URL"] =
Backurl;
Then it activates the control. If the address is the same, Backlink needs to verify whether the URL of the point you have caught before before activating the control. In OnClick (), you must read the session variable. If the session variable exists, redirect the user to the point to the control.
You can also design user controls on the visual design page. When you drag and drop the control to the web form, the ToolboxData property tells the vs.net to insert what is inserted in the ASPX file. Toolboxbitmap contains a reference to the control icon (in the form of embedded resources). As shown in Figure 1, once you add a control, the icon will appear in Toolbox.
About the Author:
Juval Lowy is an experienced software architect and is the person in charge of Idesign. This is a consultation and training company specializing in .NET design and .net transplantation. As a regional head of Microsoft in Silicon Valley, JuVal is responsible for helping to use .NET to enter into the enterprise. Recently, he wrote a name
Programming .Net Components (O'Reilly & Associates) book. You can pass
Www.ideesign.net contact him.