如何在asp.net中實現返回上一頁的功能

來源:互聯網
上載者:User
方法一:
private void Page_Load(object sender, System.EventArgs e)
        {
            
            ViewState["submittimes"]=Convert.ToInt32(ViewState["submittimes"])+1;
                        if(!Page.IsPostBack)
            {
                ViewState["submittimes"]=1;

            }
        }

頁面:
<INPUT type="button" value="Button" onclick='history.go(-<%= (int)ViewState["submittimes"] %>)'>  

方法二:
<a href=<%=request.serverVariables("Http_REFERER")%> >
    <asp:image ID="imgcancel"  Visible="false"
    ImageUrl="images/cancel.GIF"
    AlternateText="取消當前操作"  runat="server" />
</a>

方法三:

in one example application of microsoft,(i forgot the name)
using the following method
in Page_Load
if (!IsPostBack)
{
ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();
}

for go back button click
private void btnGoBack_Click(object sender, System.EventArgs e)
{
       Response.Redirect((String) ViewState["UrlReferrer"]);
}

The problem is in some cases, Request.UrlReferrer will be null if you use firewall

方法四:ASP.NET返回上一頁並重新整理

     最近在做一個網站,要讓使用者在執行了增加、刪除或修改資料的操作後轉到另一個頁面上顯示操作成功,然後再返回原來的頁面。最開始用history.go(-1),這樣能返回了,但是原來的頁面沒重新整理。因為要返回的頁面都不是相同的,所以沒法用指定URL的方法來返回,還是要獲得上一頁的URL才行。看到網上說用Response.redirect(request.servervariables["http_referer"]) 這個可以,就試了下,結果不行。百度了下才知道這個在好多種情況下是無法正常取值的,我是用Response.redirect來轉向的,所以request.servervariables["http_referer"]取來的值為空白。看來還得再想別的辦法,繼續百度之,看到有說用Request.UrlReferrer來的。照著搜到的方法一試,終於成功實現返回並重新整理了~~

 程式碼
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.UrlReferrer != null)
                ViewState["UrlReferrer"] = Request.UrlReferrer.ToString();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["UrlReferrer"].ToString());
    }

最後附上Request.ServerVariables取值的一些說明
下列情況是從瀏覽器的地址欄正常取得Request.ServerVariables("HTTP_REFERER")的:

1.直接用<a href>
2.用Submit或<input type=image>提交的表單(POSTorGET)
3.使用Jscript提交的表單(POSTorGET)
下面我們再看看Request.ServerVariables("HTTP_REFERER")不能正常取值的情況:
1.從收藏夾連結
2.單擊'首頁'或者自訂的地址
3.利用Jscript的location.hreforlocation.replace()
4.在瀏覽器直接輸入地址
5.<%Response.Redirect%>
6.<%Response.AddHeader%>或<meta http-equiv=refresh>轉向
7.用XML載入地址

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.