Solved

ASP.NET back to previous page button?

Posted on 2009-07-01
10
854 Views
Last Modified: 2013-12-17
Hi,

Is it possible to create an asp.net button on a web page that will allow a user to navigate back to the previous page?

Thanks.
0
Comment
Question by:Shepwedd
  • 5
  • 5
10 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 24755060
In the Page_Load try this:
if (!Page.IsPostBack && Page.Request.UrlReferrer!=null)
        {  
            btnPrevious.PostBackUrl = Page.Request.UrlReferrer.AbsolutePath;
        }
0
 

Author Comment

by:Shepwedd
ID: 24755587
I added the attached code as suggested but I get the error: "Operator '=' incompatible with operand types 'Int32' and 'Object' "

The ID of my button on the frontend is "BackButton"

Thanks.
protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack && Page.Request.UrlReferrer != null)

        {

            BackButton.PostBackUrl = Page.Request.UrlReferrer.AbsolutePath;

        }

    }

Open in new window

0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24755652
Is the error due to that code...not sure...it could be for some other code in your page.
0
 

Author Comment

by:Shepwedd
ID: 24755779
So the above code is correct?

The page I am trying to navigate back to is populated through a query string variable (http://www.page.aspx?variable=1), could this be causing an issue? I can press the back button on the browser to navigate back but I was hoping to create a button on the page to do the same for friendlier navigation.

Thanks.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24755950
The querystring parameter is lost as.
How are you navigating to Page2 from page1?

Page1 --> Page2-->Page1
I would suggest you to store the querystring parameter tht Page1 takes in Session before your navigate to Page2. Upon return to Page1 i.e. page1 Load ...check for querystring param...if it is null get the value from Session...
Now you have to be careful ...if Session is null then you should take appropriate step.

Or you can do is pass the querystring param to Page2...
Retrieve it on page2...
append it to Page.Request.UrlReferrer.AbsolutePath and then assign it to PostBackUrl property
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Shepwedd
ID: 24761299
I am navigating to page 2 from page 1 through the below link:

<a href="applicantInterviewInterviewers.aspx?interviewid=<%#Eval("InterviewID")%>"  target="popup" onclick="return wopen_popup(this.href,this.target, 900, 500)">Interiewers</a>

I do I save a parameter in session? Code would be appreciated.

Thanks.
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 24764192
Looks you are opening that page in new window....and then you want to go back to Parent page...won't that keep two pages open?

Here is something you can do...on Page2 Load:

 if (!Page.IsPostBack && Page.Request.UrlReferrer!=null)
        {
            string queryparam = String.Empty;
            string url = Page.Request.UrlReferrer.AbsolutePath;
            if(Page.ClientQueryString!=null)
            {  /* Another way to create postBackUrl
                queryparam = Request.QueryString["interviewid"].ToString();
                url = url + "?interviewid="+queryparam;
                 */
                url = url + "?"+ Page.ClientQueryString;
            }
            Button1.PostBackUrl = url;
        }

0
 

Author Comment

by:Shepwedd
ID: 24764798
Page 1 is my parent page.
Page 2 is a popup window
Page 3 is a popup window

I want to be able to go back to page 2 from page 3 by means of a button.
0
 

Author Comment

by:Shepwedd
ID: 24764834
With your suggested code I get the error: "Operator '=' incompatible with operand types 'Int32' and 'Object'"??

Thanks.
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 24765084
Alrite Page3 is something addition to what I was considering...
Page1 to-->Page 2 to--> Page3 to--> Page 2
If this is the case then:
In Page 2 Load:
if (!Page.IsPostBack && Page.ClientQueryString != null)
        { //What you are doing here is storing the QueryString in Session so you can retrieve in in Page3
           //Append it to PostBackUrl of btnPrevious on Page3 Load
                Session["interviewid"] = Page.ClientQueryString.ToString();
        }


Page 3 Load:
if (!Page.IsPostBack && Page.Request.UrlReferrer!=null )
        {
            string url = Page.Request.UrlReferrer.AbsolutePath;
            if (Session["interviewid"] != null)
            {    url = url + "?" + Session["interviewid"].ToString();
            }
            btnPrevious.PostBackUrl = url;
        }

Hope this works as per requirement.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now