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);


        }


 


    }


<P style="FONT-SIZE: 11pt; BACKGROUND: white; MARGIN: 0px; COLOR: black; FONT-FAMILY: Courier New" }

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

This entry was posted in ASP.NET. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://ebersys.blogspot.com Eber Irigoyen

    wouldn’t any button link with an assigned by empty OnClick event handler do the job?

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    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.

  • http://blogs.ugidotnet.org/simone%20busoli simone_b

    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?

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    @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 !

  • http://www.firoz.name Firoz Ansari

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

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

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

  • Nirav

    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.

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    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

    Really it’s nice.
    with the single line it is working.
    thanks a lot.

  • J.C.

    Usefull!! Thx!

  • http://www.antferr.com Antonio Ferraioli

    It’s ok..also qith the single line
    Page.Response.Redirect(Page.Request.Url.ToString(), true);

    ;-)

  • jayesh shah

    its really helpfull to my program
    nice work dude

  • http://www.dotnetsync.com John

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

  • zohre

    thanks alotttttttttttt, it was so useful….

  • Anand

    Simple and usefull…
    Thanks

  • motomojo

    Thanks. Helped me solve a problem.

  • CeKo

    Thanks, helped me! :)

  • http://f d

    d

  • http://www.goldengel.ch Timo

    Hey thanks. This was pretty simple and cool.

  • Master

    Thanks a million. Just what I needed

  • Gauravjoshi

    Thanks its work for me

  • Akin Ayan

    Thanks very much

  • Parjanyaroy

    thnk u ….  ”
    Page.Response.Redirect(Page.Request.Url.ToString(), true);” … was just the code i was looking for 

    cheers :) :)  
    ~PJ