Pages

Wednesday, July 1, 2009

Auto Refresh of ASP.NET Page

Update DataGrid Contents Periodically

How can I set an ASP.NET Web Page to auto refresh on a set interval.  I want a DataGrid refreshed every five minutes.

Obviously, there are several methods, including Java Script.  But, after searching the web for several minutes without finding an easy solution, my partner, Bill Ryan told me how to do this simple trick.  It was only complicated by the fact that I have other controls on the page beside the DataGrid.

To simply refresh the web page every five minutes, you can place the 
"meta http-equiv...." line shown below in the HTML editor of your ASPX page.  Content ="300" means refresh every 300 seconds or five minutes.

   <HEAD>
      <title>WebForm1title>
      <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
      <meta content="C#" name="CODE_LANGUAGE">
      --The next line of code will refresh the page every 5 minutes>
      <meta http-equiv="refresh" content="300">
      <meta content="JavaScript" name="vs_defaultClientScript">
      <meta content="http://schemas.microsoft.com/intellisense/ie5" 
           name="vs_targetSchema">
   HEAD>


That's all there is to it, if there is only a data grid on the page!  However, be aware that this refresh acts like an initial page load, and any other controls, such as Drop Down Lists, etc., which have been set to other than default values will be lost unless you do some additional coding to restore their content at the time the refresh fires.

Fortunately, I had saved the contents of three Drop Down Lists, and the value of a CheckBox in Session Variables before I implemented this code.  Therefore, I only have to call a new method, from the Page_Load event, to restore the controls.

    /// 
    /// This method will fill the controls back from session variables.
    /// Called because auto refresh is imitating an initial page load.
    /// 
    private void ResetControlsFromSessionVariables()
    {
      
// turn of autopostback so the controls won't fire
      this.cbFacility.AutoPostBack=false;
      
this.cbFacility.SelectedIndex = 
         cbFacility.Items.IndexOf(cbFacility.Items.FindByText(
         (
string)Session["FacilityName"]));
      
this.cbSortByList.AutoPostBack=false;
      
this.cbSortByList.SelectedIndex = 
         cbSortByList.Items.IndexOf(cbSortByList.Items.FindByText(
         (
string)Session["SortField"]));
      
this.cbStatusFilter.AutoPostBack=false;
      
this.cbStatusFilter.SelectedIndex = 
         cbStatusFilter.Items.IndexOf(cbStatusFilter.Items.FindByText(
         (
string)Session["StatusFilter"]));
      
this.chkPaging.AutoPostBack=false;
      
if((string)Session["Paging"]=="YES")
         
this.chkPaging.Checked=true;
      
else
        this.chkPaging.Checked=false;
      
// turn the controls back on
      this.cbFacility.AutoPostBack=true;
      
this.cbSortByList.AutoPostBack=true;
      
this.cbStatusFilter.AutoPostBack=true;
      
this.chkPaging.AutoPostBack=true;
    }

The DataGrid was already being refreshed on Page_Load, according to the selections in the controls being manipulated in the code shown above.  The reason the code shown above uses the long lines of code to set the selected index is that, unlike a Windows ComboBox, the ASP.NET DropDownList does not contain a Text property, and the SelectedIndex.Text property is ReadOnly.

0 comments:

Post a Comment