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> |
| /// <summary> /// 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; } |