Solved

ASP.NET back to previous page button?

Posted on 2009-07-01
10
860 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 63
Activating .NET Framework 3.5 and which option to choose 5 55
Removing Version of .NET in Windows 10 17 119
Convert a string to date 4 40
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

732 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