开发者

Get the previous page name in asp.net

lets say that i have 3 pages, (origin1.aspx)/(origin2.aspx)/(destination.aspx), and 开发者_如何学Pythoni have a control which represents back button, its functionality is to get from which page it has been called. and once clicked redirects back to the original caller page, i have searched the web, and found many great and simple ideas, such as queryString, and sessions, but unfortunatly i must not use any of them, so any help ?


Take a look at Request.UrlReferrer.


You could use a bit of JavaScript:

<asp:button id="m_BackButton" runat="server" onclientclick="goBack()" />

<script type="text/javascript">
  function goBack(){
    history.go(-1);
  }
</script>


You could go all clients side with javascript, but if you need to go to the server:

I don't think it is realy pretty but it gets the job done. Also gives you a lot of power.

First in the master page we have code like this so set the page name in a session

if (!IsPostBack)
{
   if (Request.UrlReferrer != null && Request.UrlReferrer.AbsoluteUri != null)
   {
      Session.Add("UrlReferrer", Request.UrlReferrer.AbsoluteUri);
   }
}

Then we have a ashx backhandler with simple code like this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1
{
    /// <summary>
    /// Summary description for Backhandler
    /// </summary>
    public class Backhandler : IHttpHandler
    {
        private const string DEFAULTPAGE = "MyDdefaultreturnpage.aspx";

        public void ProcessRequest(HttpContext context)
        {
            string previousPage = context.Session["UrlReferrer"] as String  ?? DEFAULTPAGE;
            context.Response.Redirect(previousPage);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

All the backbuttons in the app can do a redirect to the backhandler.ashx files. Yuo could even put this in your css.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜