Saeedouv
Saeedouv

Reputation: 437

Get the previous page name in asp.net

lets say that i have 3 pages, (origin1.aspx)/(origin2.aspx)/(destination.aspx), and i 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 ?

Upvotes: 4

Views: 4194

Answers (3)

KeesDijk
KeesDijk

Reputation: 2329

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.

Upvotes: 0

Zhaph - Ben Duguid
Zhaph - Ben Duguid

Reputation: 26976

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>

Upvotes: 1

Amber
Amber

Reputation: 527538

Take a look at Request.UrlReferrer.

Upvotes: 4

Related Questions