Refreshing web application pages using C#

28 01 2009

This is something that should be simple, but there is no straightforward Page.Refresh strangely! This is best way I found to do it…


The zero in the second parameter is how long I would like it to wait before refreshing the page. For example, I used this code on a log out button:

protected void LogOut(object sender, EventArgs e)
Response.AddHeader("REFRESH", "0");

So when the user clicks “Log Out” the Session is abandoned, and the page is refreshed instantly, sending the user back to the log in page.

I also used this code in another application, where I needed to call a stored procedure in the database and then refresh and redirect the page. It was originally happening too quickly, so I used the following code.

Response.AddHeader("REFRESH", "2;URL=viewinvoice.aspx?tid=" + TID);

So there you go, there are probably a lot more funky things that can be done with Response.AddHeader, but for me, it is the best way to refresh a page I have come across so far!




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: