[已解决] Asp.net计时器使用C#问题 [英] [Solved]Asp.net Timer using C# problem
问题描述
我有一个asp.net页面,其中包含显示当前时间的LABEL控件.
我使用的方式是,对于每个计时器TICK事件,将label.text设置为Now Time,
这种方式对我来说已经足够了,但是当我向下滚动到页面末尾时,它会在60秒后自动转到页面开头,因此我什至无法查看页面末尾的详细信息.
我可以做些什么来避免这种情况,这也会影响页面中的某些文本字段.
我正在为计时器和其他控件使用单独的更新面板
请帮助我
I have a asp.net page containing a LABEL control showing current time.
The way i used for it is,for each timer TICK event,set label.text as Now Time,
The way is enough for me,but when i scroll down to end of the page,its automatically goto beginning of the page after 60 seconds,so that i cant even view the details in the end of the page.
what i can do to avoid this,this also affecting some text fields in the page.
i am using seperate update panel for Timer and other controls
Please help me
推荐答案
这种情况是由于使用jQuery或使用更新面板来回发页面而避免的.
此 [
This is happening due to page post back either use jQuery or use update panel to avoid this.
this[^]will help you
--Pankaj
您应该控制自己的页面
You should control your page
if (!IsPostBack)
{}
此属性.
可能会有所帮助,
Theingi Win
this properties.
May be helpful,
Theingi Win
嗨
您要显示服务器时间还是客户端时间?替代计时器控制.
这是一些使用javascript和ajax显示客户端和服务器时间的示例.
如图所示,在aspx页面中有一个div部分.
Hi
You want to show server time or client time?. Alternatively to timer control.
Here is some example showing both client and server time using javascript and ajax.
Have a div sections in the aspx page as shown.
<div><label >Client Time</label><div id="time2"></div></div>
<div><label>Server Time</label>
<asp:TextBox runat="server" ClientIDMode="Static" ID="time1" EnableViewState="false">
</div>
在标头部分中有此javascript.如果您使用母版页,则在母版页眉部分.
Have this javascript in the header section. If you use master pages then in the master header section.
<script type="text/javascript">
function getServerTime() {
var timebox = document.getElementById('time1');
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
timebox.innerHTML = xmlhttp.responseText;
}
else {
}
}
xmlhttp.open("GET", "Default2.aspx", true);
xmlhttp.send();
}
function showTime() {
var d = new Date();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
var curr_seconds = d.getSeconds();
if (document.getElementById('time2') != null) {
document.getElementById('time2').innerHTML = curr_hour + " : " + curr_min + " : "+curr_seconds;
}
}
setInterval(showTime, 1000); //runs every 1 sec in a separate thread
setInterval(getServerTime, 1000);//runs every 1 sec in a separate thread
function test() {
showTime();
getServerTime();
}
</script>
setInterval
函数在单独的线程中运行.这样就不会不必要地干扰您的页面.
添加一个新的Webform,例如Default2.aspx,然后在该页面中加载事件
The setInterval
function run in separate thread. So it won''t interfere with your page unnecessarily.
Add a new webform say Default2.aspx and in that page load event
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToString("H : mm : ss"));
}
Default2.aspx除页面指令
外不应具有任何标记.
getServerTime
javascript函数将每1秒使用ajax调用一次.
在身体负荷事件上调用这两个函数. <body onload="test();">
希望对您有帮助
The Default2.aspx should not have any markup except the page directive
This will be called by the getServerTime
javascript function using ajax every 1 sec.
on the body load event call both functions. <body onload="test();">
Hope this helps
这篇关于[已解决] Asp.net计时器使用C#问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!