快乐的生活……

导航

一个服务器端的后退控件

// ?2003 IDesign Inc. All rights reserved
//Questions? Comments? go to
//http://www.idesign.net

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace WebControlsEx
{
 /// <summary>
 /// Sends user to the URL of the client's previous request that linked to the current URL.
 /// </summary>
   [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);
      }
   }
}

posted on 2005-03-22 08:52  小诈  阅读(559)  评论(0)    收藏  举报