auto refresh web page

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hello,
I am looking for a way to auto refresh a web page that I created, but
also let the user choose to stop the auto refresh. I can not figure out how
to stop the auto refresh. Any help would be appreciated.

Thanks,
Brian
 
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Me.CheckBox1.Checked Then
Context.Response.AddHeader("REFRESH", "2")
End If
End Sub

Set CheckBox1.AutoPostBack= True
 
Hi Brian,

you can use too:
Enable Auto-Refresh (5 seconds):
Response.Write("<META HTTP-EQUIV=Refresh CONTENT='5'>")
Disable Auto-Refresh:
Response.Write("<META HTTP-EQUIV=Refresh CONTENT='5'>")

I hope that helps.

Jorge Serrano Pérez
MVP VB.NET
 
I'm sorry, my fingers have been too fast! O:-)

Disable Auto-Refresh;
Response.Write("<META HTTP-EQUIV=Refresh CONTENT=''>")

Now it's ok.
 
Thanks. I only have one problem. I have a button that I change the text of
from Start to Stop and vice versa, but the code only sees the first setting
"Stop" The form updates, but the code never sees the new value "Run". I
hope that was clear enough. Any Ideas?

Thanks,
Brian
 
Hi Brian,

Though the <META HTTP-EQUIV=Refresh CONTENT ... > html header is ok for
contantly refreshing a page, but since you also need to let the enduser
start and stop the refreshing, I think maybe use javascript function to
auto refresh(in fact we do it via submit the page) is better. We can just
use the
"window.setInterval()" and "document.forms[0].submit()" to autopost the
page. And since the
"window.setInterval()" will return a timeid , we can store it in a page
scope javascript variable and then when user want to stop the autopostback,
we just cancel the Interval via this timeid. Also, we need to put an
additioal html intput hidden element to store the current state (start or
stop) so that when the after post back, we can remain the page's last
state. Anyway, to make it clear, here is a simple demo page I've made, you
can have a test on your side to see whether it works:

===============aspx page===================
<HTML>
<HEAD>
<title>autorefresh</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
var tid;



function ControlRefresh(btn)
{
var hd = document.getElementById("hdState");
if(btn.value == "Start")
{
tid = window.setInterval("AutoRefresh()",3000);
btn.value = "Stop";
hd.value = "on";
}
else
{
window.clearInterval(tid);
btn.value = "Start";
hd.value = "off";
}
}

function AutoRefresh()
{
document.forms[0].submit();
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td><INPUT id="btnControl" type="button" value="Start"
onclick="ControlRefresh(this)"></td>
</tr>
<tr>
<td><INPUT id="hdState" type="hidden" name="hdState" runat="server"
value="off"></td>
</tr>
<tr>
<td>
<asp:Button id="btnPostBack" runat="server" Text="Server Button Post
Back"></asp:Button></td>
</tr>
</table>
</form>
</body>
</HTML>

=============code behind=================
public class autorefresh : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnPostBack;
protected System.Web.UI.HtmlControls.HtmlInputHidden hdState;

private void Page_Load(object sender, System.EventArgs e)
{
if(IsPostBack)
{
Response.Write("<br>Page_Load fired at: " +
DateTime.Now.ToLongTimeString());

if(hdState.Value == "on")
{
Page.RegisterStartupScript("init_refresh","<script
language='javascript'>document.getElementById('btnControl').click();</script
}
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.btnPostBack.Click += new
System.EventHandler(this.btnPostBack_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnPostBack_Click(object sender, System.EventArgs e)
{
Response.Write("<br>Server Button is clicked at: " +
DateTime.Now.ToLongTimeString());
}
}
==============================

Hope helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Thanks for the reply. Unfortunatly I had to rush the program into
production, so I ended up making a second web page the was a copy of the
first with out the counter. I will give this a try though and see if I can
improve what I have now. Thanks. Brian

Steven Cheng said:
Hi Brian,

Though the <META HTTP-EQUIV=Refresh CONTENT ... > html header is ok for
contantly refreshing a page, but since you also need to let the enduser
start and stop the refreshing, I think maybe use javascript function to
auto refresh(in fact we do it via submit the page) is better. We can just
use the
"window.setInterval()" and "document.forms[0].submit()" to autopost the
page. And since the
"window.setInterval()" will return a timeid , we can store it in a page
scope javascript variable and then when user want to stop the autopostback,
we just cancel the Interval via this timeid. Also, we need to put an
additioal html intput hidden element to store the current state (start or
stop) so that when the after post back, we can remain the page's last
state. Anyway, to make it clear, here is a simple demo page I've made, you
can have a test on your side to see whether it works:

===============aspx page===================
<HTML>
<HEAD>
<title>autorefresh</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
var tid;



function ControlRefresh(btn)
{
var hd = document.getElementById("hdState");
if(btn.value == "Start")
{
tid = window.setInterval("AutoRefresh()",3000);
btn.value = "Stop";
hd.value = "on";
}
else
{
window.clearInterval(tid);
btn.value = "Start";
hd.value = "off";
}
}

function AutoRefresh()
{
document.forms[0].submit();
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td><INPUT id="btnControl" type="button" value="Start"
onclick="ControlRefresh(this)"></td>
</tr>
<tr>
<td><INPUT id="hdState" type="hidden" name="hdState" runat="server"
value="off"></td>
</tr>
<tr>
<td>
<asp:Button id="btnPostBack" runat="server" Text="Server Button Post
Back"></asp:Button></td>
</tr>
</table>
</form>
</body>
</HTML>

=============code behind=================
public class autorefresh : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnPostBack;
protected System.Web.UI.HtmlControls.HtmlInputHidden hdState;

private void Page_Load(object sender, System.EventArgs e)
{
if(IsPostBack)
{
Response.Write("<br>Page_Load fired at: " +
DateTime.Now.ToLongTimeString());

if(hdState.Value == "on")
{
Page.RegisterStartupScript("init_refresh","<script
language='javascript'>document.getElementById('btnControl').click();</script
}
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.btnPostBack.Click += new
System.EventHandler(this.btnPostBack_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnPostBack_Click(object sender, System.EventArgs e)
{
Response.Write("<br>Server Button is clicked at: " +
DateTime.Now.ToLongTimeString());
}
}
==============================

Hope helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Hi Brian,

Thanks for your followup. Well, if you meet any further problem in the
furture or need any other assistance, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top