基于Ajax:实时刷新数据,实时显示状态,局部刷新的示例。

aspx页面:

_Default为类名称,GetServiceStatus为类中的一个方法。

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Untitled Page</title>
    
<script language=Javascript>
        
       
function SetMessage()
       
{
        _Default.GetServiceStatus(GetMessage_CallBack);
       }

        
       
function GetMessage_CallBack(response)
       
{
        
var btn = document.getElementById('<%=btnSet.ClientID%>');
        
var btnPanel = document.getElementById('btnPanel');
        
var lbl = document.getElementById('lblStatus');
        
var status = response.value;
        
switch(status)
        
{
            
case "Running":
                lbl.innerText 
= "运行中";
                btn.value 
= "停止";
                btnPanel.style.visibility 
= "visible";
                
break;
            
case "Stopped":
                lbl.innerText 
= "已停止";
                btn.value 
= "启动";
                btnPanel.style.visibility 
= "visible";
                
break;
            
case "StartPending":
                lbl.innerText 
= "正在启动";
                btnPanel.style.visibility 
= "hidden";
                
break;
            
case "StopPending":
                lbl.innerText 
= "正在停止";
                btnPanel.style.visibility 
= "hidden";
                
break;
        }

       }

       

    
</script>
</head>
<body onload="setInterval('SetMessage()',100);">
    
<form id="form1" runat="server">
    
<table>
        
<tr><td width="250"><div id="lblStatus"></div></td><td>
            
<div id="btnPanel" style="visibility:visible"><asp:Button ID="btnSet" runat="server" Text="Button" OnClick="btnSet_Click1" /></div></td></tr>
    
</table>
    
</form>
</body>
</html>


.cs文件

protected void Page_Load(object sender, EventArgs e)
    
{
        Ajax.Utility.RegisterTypeForAjax(
typeof(_Default));       
    }


    [Ajax.AjaxMethod()]
    
public string GetServiceStatus()
    
{
        
return ctl.Status.ToString();
    }
posted @ 2007-04-11 15:26  Ready!  阅读(2209)  评论(0编辑  收藏  举报