Home > OS >  Redirecting to previous page in asp.net
Redirecting to previous page in asp.net

Time:10-19

I have a web form which contains submit button which updates the database. after clicking the button I need to display a message and go back to the previous page after 2 seconds. Need Help. Thanks.

CodePudding user response:

Regardless on technologies you use, you can simply store "back link" in the URL as one of the GET parameters and then use this URL to redirect user back to the required page. For example, your link might look like this:

/action/send/?backlink=/path/to/previous/page/

Of course, this link should be URL encoded and will look like this:

/action/send/?backlink=%2Fpath%2Fto%2Fprevious%2Fpage/

You can also send this link using POST method, alongside with the query for the database you might send. After the link being passed to the backend, you can place this link as a destination for redirect on the page with the message you want to show. You can use redirect by <meta> tag in HTML document like this:

<meta http-equiv="refresh" content="5;url=/path/to/previous/page/" />
  • 5 — Delay, amount of seconds before refresh will be fired;
  • url=/path/to/previous/page/ — Path for redirection.

As another solution, you can write JavaScript code which will do the same thing. Minimal implementation will look like this:

<script>
  // Wait for page to load
  window.addEventListener('load', function() {
    // Set the timer for redirection
    setTimeout(
      function () {
        location.href = '/path/to/previous/page/';
      },
      // Amount of milliseconds before refresh triggered
      5000
    );
  });
</script>
  • Related