• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

ASP.Net How to set a 5 second Response.Redirect when a button is fired.

I've searched High and low looking for a VB script that will redirect a user to another page after 5 seconds when a button is clicked.  All I seem to find is javascript and META tag refreshes that fire events 5 seconds after a page is loaded.  I'd really rather not have a whole separate file just to perform this action.  Any ideas?  No external links please, VB samples only if possible
0
Mose_
Asked:
Mose_
1 Solution
 
raterusCommented:
Is this what you want?

Sub Button_Click(...)
  System.Threading.Thread.Sleep(5000)
  Response.Redirect("somepage.aspx")
End Sub
0
 
softplusCommented:
I'd prefer not to do it on the server side, it's a waste of resources. Do the following:

Sub Button_Click(...)
   Response.AppendHeader("Refresh", "5;somepage.aspx")
End Sub

After clicking the button, it will return to the page you had open, now with the meta-refresh tag in the header to redirect to the other page after 5 seconds. However, this is dependant of the client/browser, so there's a possibility it can break. But why should you want to wait? The web's slow as it is anyway :)
John
0
 
softplusCommented:
You could also do this:
Sub Button_Click(...)
  Response.Redirect("gosomewhere.aspx?page=somepage.aspx")
End Sub

and on the gosomewhere.aspx, you get the desired URL, add the Refresh= metatag into the header, show a short text, e.g. "Redirecting to ... just a moment please ....  ". It would make it more transparent for the user.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
raterusCommented:
Actually, when you "sleep", asp.net tells the server to go do something else for that 5 seconds and come back to me then, so you really aren't wasting any resources.  I do prefer John's solution though, my solution will make it look like the page took 5 seconds to load, at least with John's the page will be immediately returned, where you can give them information like "blah blah, will be redirected in 5 seconds..."
0
 
nauman_ahmedCommented:
I will recommend you use the Timers class as it will be more efficient. Using Thread.Sleep() will block the thread to perform any kind of functions:

URL: http://www.asp101.com/samples/time_aspx.asp


Example:
---------------
URL: http://www.411asp.net/home/tutorial/specific/timers/classes/timer

private void StartTimer()
{
    System.Timers.Timer myTimer = new System.Timers.Timer();
    myTimer.Interval = 5000;
    myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed);
    myTimer.Enabled = true;
}

protected void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    //do some work
}

HTH, Nauman.

0
 
Mose_Author Commented:
Thanks guys, I'm trying to put a couple of different conditions for when I exit a page to return to the main datagrid.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now