Results 1 to 6 of 6

Thread: Back Button Event?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    The Twilight Zone
    Posts
    295

    Back Button Event?

    Hi,

    Is it possible to add an event to a button on a web form so when it is clicked it goes back to the previous page. So it simply replicates the back button you find on a explorer browser. I want this so i can have all the values in a form still entered, i know i could do this using sessions etc but it would be much simplier to have that back button?

    Thanks

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Back Button Event?

    You redirect to the referrer page which does the same thing.

    Code:
    void button_click(object sender, EventArgs e) {
        Response.Redirect(Request.UrlReferrer.ToString());
    }
    HTH

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    The Twilight Zone
    Posts
    295

    Re: Back Button Event?

    Using a Response.Redirect method will cause my form fields to be empty once the button is clicked. But with a Browser back button, you still have your data on the original page?

  4. #4
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: Back Button Event?

    In which case you'd have to use client-side JavaScript to actually use the browser's history.

    I believe the JavaScript needed is: history.go(-1).

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  5. #5
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Back Button Event?

    This would look like:
    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'Put user code to initialize the page here
    3.         Button1.Attributes.Add("onClick", "history.back();")
    4.     End Sub
    Alnernatively you can do this at design time:
    Code:
    <asp:Button id="Button1" OnClick="history.back();" runat="server" Text="Button"></asp:Button>
    Woof

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    The Twilight Zone
    Posts
    295

    Re: Back Button Event?

    Thanks for that, the javascript works but one problem, i have autoPostback dropdownlists on the form page, so when i get back to the form page they aren't working properly. I will probably just do it using sessions variables to re-populate the data page.

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width