.aspx:
001 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
002
003 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
004 <html xmlns="http://www.w3.org/1999/xhtml">
005 <head runat="server">
006 <title>無標(biāo)題頁</title>
007
008 <script type="text/javascript">
009
010 //向服務(wù)器傳遞參數(shù)
011 function DoSearch(){
012 var firstName=document.getElementById("TextBox1").value;
013 CallServer(firstName,"");
014 }
015
016 //得到服務(wù)器的數(shù)據(jù)
017 function ReceiveServerData(txtUserInfo){
018 Results.innerHTML=txtUserInfo;
019 }
020
021 //設(shè)置每1秒執(zhí)行一次
022 setInterval("DoSearch()",1000);
023 </script>
024
025 </head>
026 <body>
027 <form id="form1" runat="server">
028 <div>
029 姓名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
030 <br />
031 <span id="Results" style=" width:500px;"></span>
032 </div>
033 </form>
034 </body>
035 </html>
036 [/code]
037 .aspx.cs
038 [code]
039 using System;
040 using System.Collections;
041 using System.Configuration;
042 using System.Data;
043 using System.Web;
044 using System.Web.Security;
045 using System.Web.UI;
046 using System.Web.UI.HtmlControls;
047 using System.Web.UI.WebControls;
048 using System.Web.UI.WebControls.WebParts;
049 using System.Data.SqlClient;
050
051 public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
052 {
053 protected string txtUserInfo;
054
055
056 protected void Page_Load(object sender, EventArgs e)
057 {
058 //獲取一個對客戶端函數(shù)的引用
059 string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");
060 //動態(tài)注冊回調(diào)函數(shù)
061 string callbackScript = "function CallServer(arg,context)" + "{" + cbReference + "};";
062 //引發(fā)callbackScript
063 Page.ClientScript.RegisterStartupScript(this.GetType(), "CallServer", callbackScript, true);
064 }
065
066 //引發(fā)Callback事件處理
067 public void RaiseCallbackEvent(string txtFirstName)
068 {
069 if (txtFirstName != null)
070 {
071 String connString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlserver2008"].ToString();
072
073 SqlConnection conn = new SqlConnection(connString);
074
075 conn.Open();
076
077 SqlCommand comm = new SqlCommand("select * from zzx where [name]=@name", conn);
078
079 comm.Parameters.Add("@name", SqlDbType.VarChar).Value = txtFirstName;
080
081 SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
082 if (reader.Read())
083 {
084 txtUserInfo = "員工編號:" + reader["id"].ToString() + "<br>";
085 txtUserInfo += "員工姓名:" + reader["name"].ToString() + "<br>";
086 txtUserInfo += "地址:" + reader["address"].ToString() + "<br>";
087 txtUserInfo += "服務(wù)器查詢時間:" + DateTime.Now.ToString();
088 }
089 else
090 {
091 if (string.IsNullOrEmpty(txtFirstName))
092 {
093 txtUserInfo = "請輸入姓名";
094 }
095 else
096 {
097 txtUserInfo = "查無此人";
098 }
099 }
100
101 comm.Dispose();
102 reader.Dispose();
103 conn.Dispose();
104 }
105 }
106
107 //得到回調(diào)的結(jié)果,返回給客戶端
108 public string GetCallbackResult()
109 {
110 return txtUserInfo;
111 }
112
113
114 }
簡化版(偷懶一下):
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head runat="server">
06 <title>無標(biāo)題頁</title>
07
08 <script type="text/javascript">
09 function OnCallBack(txtUserInfo,context){
10 Results.innerHTML=txtUserInfo;
11 }
12 </script>
13
14 </head>
15 <body>
16 <form id="form1" runat="server">
17 <div>
18 姓名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
19 <input id="Button2" type="button" value="button"
20 onclick="<%=Page.ClientScript.GetCallbackEventReference(this, "document.getElementById('TextBox1').value", "OnCallBack",null)%>" />
21 <br />
22 <span id="Results" style="pink; width: 500;"></span>
23 </div>
24 </form>
25 </body>
26 </html>
27 .aspx.cs
28
29 using System;
30 using System.Collections;
31 using System.Configuration;
32 using System.Data;
33 using System.Web;
34 using System.Web.Security;
35 using System.Web.UI;
36 using System.Web.UI.HtmlControls;
37 using System.Web.UI.WebControls;
38 using System.Web.UI.WebControls.WebParts;
39 using System.Data.SqlClient;
40 using System.Text;
41 public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
42 {
43 protected StringBuilder txtUserInfo;
44
45 protected void Page_Load(object sender, EventArgs e)
46 {
47
48 }
49
50 public string GetCallbackResult()
51 {
52 return txtUserInfo.ToString();
53 }
54
55 public void RaiseCallbackEvent(string txtFirstName)
56 {
57 txtUserInfo = new StringBuilder();
58 String connString = ConfigurationManager.ConnectionStrings["sqlserver2008"].ToString();
59 SqlConnection conn = new SqlConnection(connString);
60 conn.Open();
61 SqlCommand comm = new SqlCommand("select * from zzx where [name]=@name", conn);
62 comm.Parameters.Add("@name", SqlDbType.VarChar).Value = txtFirstName;
63 SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
64 if (reader.Read())
65 {
66 txtUserInfo.Append("員工編號:" + reader["id"].ToString() + "<br>");
67 txtUserInfo.Append("員工姓名:" + reader["name"].ToString() + "<br>");
68 txtUserInfo.Append("地址:" + reader["address"].ToString() + "<br>");
69 txtUserInfo.Append("查詢時間:" + DateTime.Now.ToString());
70 }
71 else
72 {
73 if (txtFirstName == string.Empty)
74 {
75 txtUserInfo.Append("請輸入姓名");
76 }
77 else
78 {
79 txtUserInfo.Append("查無此人");
80 }
81 reader.Dispose();
82 comm.Dispose();
83 conn.Dispose();
84 }
85 }
86 }
示例3:
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head runat="server">
07 <title>無標(biāo)題頁</title>
08 <script type="text/javascript">
09 //客戶端執(zhí)行的方法
10 //下面的方法是接收并處理服務(wù)器方法返回的結(jié)果
11 function Success(args,context){
12 message.innerHTML=args;
13 }
14
15 //下面的方式是當(dāng)接收服務(wù)器方法處理的結(jié)果發(fā)生異常時調(diào)用的方法
16 function Error(){
17 message.innerHTML="發(fā)生了異常!";
18 }
19 </script>
20 </head>
21 <body>
22 <form id="form1" runat="server">
23 <div>
24 用戶名:<input type="text" id="txtUserName" onblur="CallServerMethod(txtUserName.value,null)" />
25 <span id="message"></span>
26 <br />
27 密碼:<input type="password" size="10" maxlength="20" id="txtPwd" />
28 </div>
29 </form>
30 </body>
31 </html>
32 [code]
33 public partial class Default3 : System.Web.UI.Page,ICallbackEventHandler //實現(xiàn)ICallbackEventHandler接口
34 {
35
36 String result = String.Empty;
37
38 protected void Page_Load(object sender, EventArgs e)
39 {
40 //獲取當(dāng)前頁的ClientScriptManager的引用
41 ClientScriptManager csm = Page.ClientScript;
42 /*獲取回調(diào)的引用.會在客戶端生成WebForm_DoCallback方法,
43 * 調(diào)用它來達到異步調(diào)用.這個方法是微軟寫的方法,會被發(fā)送
44 到客戶端*/
45 /*注意這里的"Success"和Error兩個字符串分別是客戶端代碼中
46 *定義的兩個javascript函數(shù)*/
47 //下面的方法最后一個參數(shù)的意義:true表示執(zhí)行異步回調(diào),false標(biāo)志執(zhí)行同步回調(diào)
48 String reference = csm.GetCallbackEventReference(this, "args", "Success", "", "Error", true);
49 String callbackScript = "function CallServerMethod(args,context){\n"+
50 reference+";\n }";
51 //向當(dāng)前頁面注冊javascript腳本代碼
52 csm.RegisterClientScriptBlock(this.GetType(), "CallServerMethod",callbackScript,true);
53 }
54
55 #region ICallbackEventHandler 成員
56
57 /// <summary>
58 /// 返回回調(diào)方法執(zhí)行結(jié)果的方法
59 /// </summary>
60 public string GetCallbackResult()
61 {
62 return result;
63 }
64
65 /// <summary>
66 /// 在服務(wù)器端運行回調(diào)方法
67 /// </summary>
68 public void RaiseCallbackEvent(string eventArgument)
69 {
70 if (eventArgument.ToLower().IndexOf("admin")!=-1)
71 {
72 result =eventArgument+ "不能作為用戶注冊.";
73 }
74 else
75 {
76 result = eventArgument + "可以注冊.";
77 }
78 }
79
80 #endregion
81 }
更多信息請查看IT技術(shù)專欄