2013年6月17日 星期一

ASP.NET Request.UrlReferrer取得上一頁的網址

ASP.NET Request.UrlReferrer取得上一頁的網址


HTML:
<form id="form1" runat="server">
<div>
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Test_tsUrlReferrer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            ViewState["UrlReferrer"] = "";
            if (!IsPostBack)
            {
                if (Request.UrlReferrer != null)
                    ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();
            }
            Label1.Text = ViewState["UrlReferrer"].ToString();
        }
        catch (Exception ex)
        {
            throw ex;
        }
     
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect(ViewState["UrlReferrer"].ToString());
        }
        catch (Exception)
        {
         
            throw;
        }
     
    }
}

沒有留言:

張貼留言