Thursday, April 7, 2011

How to refresh a ASP.Net Page from CodeBehind ? OR Automatically Refresh aspx Page at Regular Intervals

We can refresh a webpage with the help of meta tags at regular intervals. When we place the below meta tag in head tag, it will refresh your webpage automatically every 100 seconds.
<meta http-equiv="refresh" content="100">

At times, we might want to conditionally refresh an ASPX page automatically from codebehind file.
With the introduction of ASP.Net 2.0, we can set Meta tags dynamically from codebehind class using HtmlMeta class.

To refresh our aspx page from the codebehind class we can use the following code,

public void RefreshPage()
    {
HtmlMeta meta = new HtmlMeta();
HtmlHead head = (HtmlHead)Page.Header;
meta.HttpEquiv = "refresh";
meta.Content = "5";
head.Controls.Add(meta);
}


The above code will emit appropriate meta tags similar to above to refresh the page automatically every 5 seconds.

No comments:

Post a Comment

Ajax CalendarExtender displaying at wrong position in Chrome

< script type ="text/javascript" language ="javascript">     function onCalendarShown(sender, args)...