CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

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


Published May 18 2006, 04:37 AM by pvanooijen
Filed under:

Comments

Jason Haley said:

# May 18, 2006 11:08 AM

Eber Irigoyen said:

wouldn't any button link with an assigned by empty OnClick event handler do the job?
# May 18, 2006 12:36 PM

pvanooijen said:

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.
# May 18, 2006 1:54 PM

simone_b said:

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?
# May 18, 2006 6:37 PM

pvanooijen said:

@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 !
# May 19, 2006 3:55 AM

Peter's Gekko said:

Yesterday I wrote a little post on a refresh button for asp.net pages. It worked but had jump through...
# May 19, 2006 3:56 AM

Firoz Ansari said:

IMO, we can easily refresh page through single server trip also:
http://www.firoz.name/2006/05/19/a-refresh-button-for-aspnet-pages/
# May 19, 2006 6:39 AM

pvanooijen said:

Firoz, you are right
The other post
http://codebetter.com/blogs/peter.van.ooijen/archive/2006/05/19/144913.aspx
describes just that.
# May 19, 2006 7:10 AM

Nirav said:

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.
# August 3, 2006 7:21 AM

pvanooijen said:

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.
# August 3, 2006 4:40 PM

Sekhar said:

Really it's nice.

with the single line it is working.

thanks a lot.

# June 23, 2007 3:22 AM

J.C. said:

Usefull!! Thx!

# September 13, 2007 8:12 PM

Antonio Ferraioli said:

It's ok..also qith the single line

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

;-)

# October 9, 2007 6:48 AM

jayesh shah said:

its really helpfull to my program

nice work dude

# May 31, 2008 5:34 AM

John said:

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

# July 23, 2008 3:42 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

This Blog

Syndication

News