sunfei

万事烦扰,皆由心生,心不动,何来纷扰!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ajax局部刷新 (三种方法都可以)

Posted on 2008-11-20 17:42  sunfei  阅读(853)  评论(0编辑  收藏  举报

---------------------------------------------------------------------------------------------------------------------------------
ajax局部刷新
<script>
setInterval("refreshTime()",1000); //每格1秒刷新一次
funciton refreshTime()
{
var dateObj = new Date();

time.innerHTML = dateObj.toLocaleDateString(); //刷新div里面的内容
}
</script>
<div id=time> </div>

---------------------------------------------------------------------------------------------------------------------------------

function doRef()
{
var url = "xxx.aspx"
var dateRequest = new ActiveXObject("Microsoft.XMLHTTP");
dateRequest.abort() ;
dateRequest.open( "GET", url , true );
dateRequest.onreadystatechange = onSendSelf ;
dateRequest.send() ;
function onSendSelf()
{
if( dateRequest.readyState != 4 )
{
return ;
}
var theDate = dateRequest.responseText ;//如果出现编码问题,可以在服务端escape一下,然后在这里使用unescape( responseText )
theDiv.innerHTML = theDate ;
}
}
setInterval( doRef , 1000 ) ;//1秒刷新一次,有点快

--------------------------------------------------------------------------------------------------------------------------------

<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
</asp:Timer>
<asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
</form>


=====在后台Timer1_Tick事件加上
// Interval="1000" 是设置刷新的时间间隔
TextBox1.Text=DateTime.Now