Browser Back via codebehind

Posted on 2007-07-25
Last Modified: 2012-08-13
Is there a way to redirect to the last page (just like clikc back on the browser) via c# codebehind?

Response.Redirect() is NOT what i am looking for. Say for example i scroll half way down a page and click a link... it posts me to another page. I then clikc back on my browser and will endup on the previous page, half way down, exactly where i left it... I need to do this via C#

Also using javascript (history.back()) produces the results i want BUT i want to do it from the codebehind.

Question by:Nugs
    LVL 20

    Accepted Solution

    I believe you would have to use javascript to store the scroll top position into a hidden field on each page.  Everytime you postback you get this value using

    Request.Form["YourHiddenFieldName"] as javascript will have populated this field, without using ajax this is the only way to retrieve this value.  Once you have obtained this value, store it into a session as previous scroll position or something along with the url maybe.  When you load and page and is not postback check the URL is what you are on and if it has a value, Register a start up script to makee the page scroll to that position using javascript;

    using ajax you can store this value to the session directly.

    LVL 2

    Author Comment

    So what you trying to tell me that .net has nothing to handle what would be handeled by javascript to do a SIMPLE back step on the browser.

    So i can even do a Attribute.Add() on a ASPX button control to do what is being done below?
    <form style="margin:0px"><input type=button value="< Back to Search Results" class="navigation_objects" onCLick="history.back()"></form>

    My issue is thatt he ASPX generaled form tag posts to itself... So i can write code that will make my <asp:button control render like the above. But the click even posts back the same page...

    What i am trying to avoid here is having to use nested <form> tags to acomplish a simple back on the browser. From what i am seeing in my search and in this post is that i have to Use a client side script to do this and i have to nest form tags to do this?

    I would rather this not become a huge thing of posting values into hidden form fields etc... I honestly can't belive that i can't have a asp:button control post a javascript call that would send the browser backa  page...

    LVL 2

    Author Comment

    I have figure out this issue.

    The key is to set the OnMouseDown event of the button rather than the OnClick event fromt he code behind.

    So like this:
    <asp:Button ID="btnBacktoResults" CausesValidation="false" runat="server" Text="< Back to Search Results" />
    btnBacktoResults.Attributes.Add("onMouseDown", "javascript: history.back()");

    As you can see, no hidden anything, no cleint side form tags nothing fancy... The issue before was that the server side OnClick event was conflicting with the client side or javascript OnClick event.

    But i thank you very much for you input, and since you where the only one kind enough to reply to my post - enjoy the points...



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    758 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

    9 Experts available now in Live!

    Get 1:1 Help Now