20 May 2006

A better refresh button for asp.net pages

4 Comments ASP.NET

Peter has modified his Refresh user control and now page will only redirect to itself that will save one server trip:
A better refresh button for asp.net pages

But this modified control will again not exact solve problem. Redirecting page will not behave like refresh button, as after redirecting to the same page you will going to get fresh instance of page which mean you will loose all values in ViewState, Hidden Field etc. which has been modified with some back post event.

To put my point, place a simple button in the page, and on its click event, add some value in ViewState:
private void Button1_Click(object sender, System.EventArgs e)
{
ViewState["Value"] = "100";
Label1.Text = (string) ViewState["Value"];
}

with that display value of this ViewState key in page load.
private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text = (string) ViewState["Value"];
}

Now if user clicks on RefreshLink control, the value displayed from ViewState will be blank and NOT 100. So it’s basically not simulating the Refresh button exactly.

I still suggest that if you submit the form, its work like Refresh button.

HyperLink1.Attributes.Add("onclick", "document.Form1.submit();");

If user clicks on HyperLink1, page will display same ViewState value which has been modified before. So its actually behaving more like Refresh button.

Here is my suggested RefreshLink control:
[ToolboxData("<{0}:MyRefreshLink runat=server></{0}:MyRefreshLink>")]
public class MyRefreshLink : HyperLink
{
protected override void OnLoad(EventArgs e)
{
base.Attributes.Add("onclick", "document.Form1.submit();");
base.OnLoad(e);
}

[Browsable(false)]
public new string NavigateUrl
{
get
{
return base.NavigateUrl;
}
}
}

written by Firoz Ansari
The author didn‘t add any Information to his profile yet.

4 Responses to “A better refresh button for asp.net pages”

  1. Peter van Ooijen says:

    Both the refresh button of the browser and my button are intended to get a completely fresh insatnce of the page. What you are trying to make is just another submit button. Every (link-)button without any code attached will do that.

  2. hema says:

    Refresh button not working in output screen

  3. hema says:

    The refresh button not work in output screen
    the code is Asp.net with c#

  4. Firoz Blog A better refresh button for asp net pages | fix my credit says:

    [...] Firoz Blog A better refresh button for asp net pages Posted by root 1 hour 1 minute ago (http://www.firozansari.com) May 20 2006 leave a comment username required email required web site comment powered by wordpress design by woo themes Discuss  |  Bury |  News | Firoz Blog A better refresh button for asp net pages [...]

Leave a Reply

Spam protection by WP Captcha-Free