Peter's Gekko

Sponsors

The Lounge

Wicked Cool Jobs

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
A refresh button for asp.net pages

Your browser has a back, forward and refresh button. Some time ago I wrote a little post on a back button to include on your own asp.net pages. According to the feedback you liked that one. Here I would like to share a refresh button. Quite simple and it works well! Although not as simple as the better version which resulted from the feedback on this one,

You work with this code just like the back-button. This is the code

using System;

using System.Collections.Generic;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.ComponentModel;

 

namespace WebControlLibrary

{

   /// <summary>

    /// Summary description for MyRefreshButton.

    /// </summary>

    [ToolboxData("<{0}:MyRefreshButton runat=server></{0}:MyRefreshButton>")]

    public class MyRefreshButton : System.Web.UI.WebControls.LinkButton

    {

        protected override void OnClick(EventArgs e)

        {

            base.OnClick(e);

            Page.Response.Redirect(Page.Request.Url.ToString(), true);

        }

 

    }

Also this one is a descendent of the linkbutton. When it's clicked it redirects the response to the url of the page itself. Which will start a new series of roundtrips. Your page will have it's initial state again.

The actual behavior is not quite the same as that of the browser's refresh buttonn

  • Before issuing the redirect any click handlers are invoked in the base.OnClick(). In there you could do some things to save work.
  • Due to the way the page lifecycle works pageload and other eventhandlers will be executed before the redirect. Using the browser button these will not fire. The page prerender will not fire.

The better version does not have this extra behavior.

Hope you like it


Posted Thu, May 18 2006 4:37 AM by pvanooijen
Filed under:

[Advertisement]

Comments

Jason Haley wrote Interesting Finds: May 18, 2006 AM edition
on Thu, May 18 2006 11:08 AM
Eber Irigoyen wrote re: A refresh button for asp.net pages
on Thu, May 18 2006 12:36 PM
wouldn't any button link with an assigned by empty OnClick event handler do the job?
pvanooijen wrote re: A refresh button for asp.net pages
on Thu, May 18 2006 1:54 PM
No. There would be a roundtrip, but the viewstate would maintain the state of the page. By redirecting the viewstate is discarded and a fresh instance is created. With the same effect as the browser's refresh button.
simone_b wrote re: A refresh button for asp.net pages
on Thu, May 18 2006 6:37 PM
You must be joking.
First it does look like you don't know much about the method you are using. If you're passing true as the second parameter you should just call the other overload, true is the default.

Then do you know what you are doing?

1. Clicking your button performs a postback
2. On the postback you are doing a Response.Redirect, that is, sending the client a 302 Found header, with the redirect coordinates.
3. The browser then requests the page again to the server.

To me using a server control for a page refresh is a bit too much. What about a HyperLink or anything else working only on the client?
pvanooijen wrote re: A refresh button for asp.net pages
on Fri, May 19 2006 3:55 AM
@Simone:
Please, I do know very well what I'm doing. Omitting the second parameter because I will pass the default value may be shorter to type but makes the code more informative.

I do know about the many hoops, that's what's described in the concluding remarks.

But you are right. It can be done a lot simpler by using a plain hyperlink. Coming up !
Peter's Gekko wrote A better refresh button for asp.net pages
on Fri, May 19 2006 3:56 AM
Yesterday I wrote a little post on a refresh button for asp.net pages. It worked but had jump through...
Firoz Ansari wrote re: A refresh button for asp.net pages
on Fri, May 19 2006 6:39 AM
IMO, we can easily refresh page through single server trip also:
http://www.firoz.name/2006/05/19/a-refresh-button-for-aspnet-pages/
Firoz Ansari » Blog Archive » A refresh button for asp.net pages wrote Firoz Ansari &raquo; Blog Archive &raquo; A refresh button for asp.net pages
on Fri, May 19 2006 6:39 AM
pvanooijen wrote re: A refresh button for asp.net pages
on Fri, May 19 2006 7:10 AM
Firoz, you are right
The other post
http://codebetter.com/blogs/peter.van.ooijen/archive/2006/05/19/144913.aspx
describes just that.
Nirav wrote re: A refresh button for asp.net pages
on Thu, Aug 3 2006 7:21 AM
I want to refresh my page in asp.net 2.0 mannually, can any one say how can i do that? Actuallly i have used 3rd party control's drop down list, in which i am selecting one value and according to it is filling other drop down's value.
pvanooijen wrote re: A refresh button for asp.net pages
on Thu, Aug 3 2006 4:40 PM
In your case you want to preserve the state of the page, I would suggest setting the autopostback of the dropdown list to true. Select a different value will postback to the server without loss of the page's state.
Sekhar wrote re: A refresh button for asp.net pages
on Sat, Jun 23 2007 3:22 AM

Really it's nice.

with the single line it is working.

thanks a lot.

J.C. wrote re: A refresh button for asp.net pages
on Thu, Sep 13 2007 8:12 PM

Usefull!! Thx!

Antonio Ferraioli wrote re: A refresh button for asp.net pages
on Tue, Oct 9 2007 6:48 AM

It's ok..also qith the single line

Page.Response.Redirect(Page.Request.Url.ToString(), true);

;-)

jayesh shah wrote re: A refresh button for asp.net pages
on Sat, May 31 2008 5:34 AM

its really helpfull to my program

nice work dude

John wrote re: A refresh button for asp.net pages
on Wed, Jul 23 2008 3:42 AM

Thanks man it helped me on one of my problemoz :O

zohre wrote re: A refresh button for asp.net pages
on Mon, Jul 28 2008 1:52 PM

thanks alotttttttttttt, it was so useful....

Anand wrote re: A refresh button for asp.net pages
on Sat, Dec 27 2008 8:54 AM

Simple and usefull...

Thanks

motomojo wrote re: A refresh button for asp.net pages
on Wed, Apr 8 2009 4:40 PM

Thanks.  Helped me solve a problem.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us