Implement a ASP.net custom back control

Source: Internet
Author: User
Tags reference
Asp.net| Control This article describes how to add a server-side control to a Web page so that the user's browser is redirected to the page that the control points to (referring page).
by Juval Lowy

Q: Implement a asp.net back control
I want to add a link to the ASP.net page, through which I can return to the page it points to. Don't know how to implement it with a server-side control? I want to go back through the control to a page that I have visited, not to use the method of browsing history.

A:
There are two ways you can implement a "back" link on a Web page. The first method is to use client script to read the record of the previously visited page and redirect it to the previous page:

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


But there are several drawbacks to this approach. The application cannot control where the user is redirected. Typically, you want to keep users in the application, rather than having them run to another page. This approach works only if the browser supports client-side scripting. The biggest drawback is that it is inconsistent with the ASP.net programming model. Asp. NET one of the biggest benefits is that it is different from the traditional ASP, it does not require you to rely on client-side scripting. All you need to know is how to use the server-side controls you write in managed code, ASP. NET will do other work for you. When using client script, the back link is not related to the rest of the application, it runs on the server, and the server-side controls are used. This makes it difficult for you to control whether the back link is activated, and it is difficult for you to control redirection because they are all based on server-side event handlers.

The second approach is to use a asp.net custom user control. The source file that is included with this article contains a backlink ASP in the Webcontrolsex Class library assembly. NET user control. If you want to use the control, you need to right-click the Web Forms Toolbox and select Add/remove Items from the pop-up context menu. Tap the. NET Frameworks Components tab and press the browse button. Select the Webcontrolsex assembly and click Open. This adds the backlink user control on the toolbox. You just drag it into your form and you can add a linked control with the Text property "back". At design time, the control has properties like a standard link button, such as a link style (see Figure 1). When the user clicks the button at run time, it directs the browser to the previous page (if any).

Implementing backlink user controls is not as simple as we can see. To build a Web user control, you must use a DLL class library. There are two ways you can provide a user control: Inherit a class called WebControl and customize it, or inherit an existing control and customize it. For the backlink control, a good way to do this is to inherit a LinkButton because the LinkButton control provides most of the features that are difficult to implement.

You can inherit a class called Backlink from the LinkButton, set the Text property to back in its constructor, and provide some meaningful default values:

public class Backlink:linkbutton
{
Public backlink ()
{
Text = "Back";
}
}


In fact, inheriting a LinkButton can be reflected in HTML. By inheriting backlink, the control can embody all LinkButton properties-such as default properties, font settings properties, and events-in the designer.

LinkButton has a protected virtual method called OnClick (), when the page is returned. NET calls the method to have the link button trigger the Click event. LinkButton must overwrite onclick () and redirect the user to the page to which the control points.

The important question is how to get the page that the current page points to on the server side within the scope of the backlink control. Luckily, ASP. NET has the function that the HttpRequest object provides a public property of a URL type, called a urlreferrer, that represents the URL of the page to which it is directed. LinkButton user controls can get a reference to a page, which is implemented through the page property of the control class. Control is the base class for WebControl, and WebControl is the base class for LinkButton. The control can also use the page property to read its HttpResponse object (the Response property), thereby redirecting the user to the page being pointed to.

However, if you implement the backlink control using the following code, 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 that asp.net tracks the way the page is pointing. For example, we take the Home.aspx page as an example, which directs the user to the Subform.aspx page. If the Subform.aspx page has a backlink control, the user clicks the link and triggers the event that returns the page to the server. The value of the Urlreferrer property on the server is "subform.aspx" (not "home.aspx"), because when the page is returned, subform.aspx is pointing to itself. The workaround is to cache the page pointed to in the session variable and redirect it to the page you point to in the OnClick () when the control is loaded.

Now you need to solve a few problems: ASP.net does not always provide a page with a guide, in which case asp.net sets the urlreferrer value to null. You also need to provide a "smart" back link, which can be redirected to the previous page logically. In other words, if a page containing backlink itself is overloaded (after some of the control's click events are processed), the back link should be able to detect this "intelligently" and redirect to the previous page of "real", not itself. Note that if you use client code, this is not possible because the back record variable renders the same page when it is loaded again.

Backlink overrides the OnLoad () method of its base class (see Listing 1). When the page containing the backlink control is loaded, the onload () is invoked. OnLoad () first to see if there are any page information to point to. If the urlreferrer is empty, then onload () will make the back link ineffective. If you have guidance information, you need to verify that the page you are pointing to is not the current page. You can do this by comparing the URL of the page you are pointing to and the URL of the request page:

if (Backurl.absolutepath!= Page.Request.Url.AbsolutePath)

If two URLs are different, backlink saves the URL of the page to a session variable:

page.session["referring URL"] = Backurl;

It then activates the control. If the address is the same, backlink before activating the control, you need to verify that the URL of the page you are pointing to has already been cached. In OnClick (), you must read the session variable and redirect the user to the page that the control points to if the session variable exists.

You can also design user controls on a visual design page. When you drag and drop a control onto the Web form, the ToolBoxData property tells Vs.net what to insert in the ASPX file. ToolboxBitmap contains a reference to the control's icon (in the form of an embedded Resource). As shown in Figure 1, once you add a control, the icon is displayed in the toolbox.

Source:

[ToolBoxData ("<{0}:backlink runat=server></{0}:backlink>")]
[ToolboxBitmap (typeof (Backlink), "Backlink.bmp")]
public class Backlink:linkbutton
{
Public backlink ()
{
Text = "Back";
ToolTip =
"Click to go to the previous page";
}

protected override void OnClick (EventArgs e)
{
Uri Backurl = (URI) page.session[
"referring URL"];
page.session["referring URL" = null;
if (Backurl!= null)
{
Page.Response.Redirect (
Backurl.absoluteuri);
}
}

protected override void OnLoad (EventArgs e)
{
Uri Backurl = Page.Request.UrlReferrer;
if (Backurl = = null)//no referrer
Information
{
Enabled = false;
Return
}
if (Backurl.absolutepath!=
Page.Request.Url.AbsolutePath)
{
page.session["referring URL"] =
Backurl;
Enabled = true;
Return
}
Else
{
Object obj = page.session[
"referring URL"];
if (obj!= null)
{
Enabled = true;
}
}
Base. OnLoad (e);
}
}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.