(c) 2003 Visual Studio Magazine 
Fawcette Technical Publications

C#	Implement the BackLink User Control
Listing 1	The BackLink control derives from LinkButton, which does the actual rendering. BackLink caches the referrer page at load time in a session variable, and it redirects the user to it by overriding OnClick(). 

[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);
	}
}

