Solved

ASP.NET back to previous page button?

Posted on 2009-07-01
10
848 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

20 Experts available now in Live!

Get 1:1 Help Now