HTML代碼
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4<html xmlns="http://www.w3.org/1999/xhtml">
5<head runat="server">
6 <title>Untitled Page</title>
7 <script language="javascript" type="text/javascript">
8 function hello()
9 {
10 BeginMessage();
11 var t=setTimeout("endMessage('計時結束 5 seconds!')",5000)
12 }
13 function endMessage(msg)
14 {
15 PageMethods.Msg(msg,updateMessages);
16 }
17 function BeginMessage()
18 {
19 PageMethods.Msg("計時開始",updateMessages);
20 }
21 function updateMessages(result)
22 {
23 $get("lbl").innerHTML =result;
24 }
25 </script>
26</head>
27<body>
28 <form id="form1" runat="server">
29 <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server" />
30 <div>
31 <asp:Label ID="lbl" runat="server" Text="Label"></asp:Label
32 ><input id="Button1" type="button"
33 value="開始" onclick="hello()" />
34 </div>
35 </form>
36</body>
37</html>
38
後台代碼
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
static public string Msg(string msg)
{
return msg;
}
}