Solved

ASP.NET back to previous page button?

Posted on 2009-07-01
10
855 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reading the Web.Config using IIS 7.5? 4 36
imap read mail 1 29
Regular expression help 2 25
Save ms data to server side. 19 46
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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