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();
}