.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>无标题页</title>
007
008 <script type="text/javascript">
009
010 //向服务器传递参数
011 function DoSearch(){
012 var firstName=document.getElementById("TextBox1").value;
013 CallServer(firstName,"");
014 }
015
016 //得到服务器的数据
017 function ReceiveServerData(txtUserInfo){
018 Results.innerHTML=txtUserInfo;
019 }
020
021 //设置每1秒执行一次
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 //获取一个对客户端函数的引用
059 string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");
060 //动态注册回调函数
061 string callbackScript = "function CallServer(arg,context)" + "{" + cbReference + "};";
062 //引发callbackScript
063 Page.ClientScript.RegisterStartupScript(this.GetType(), "CallServer", callbackScript, true);
064 }
065
066 //引发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 += "服务器查询时间:" + 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 //得到回调的结果,返回给客户端
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>无标题页</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>无标题页</title>
08 <script type="text/javascript">
09 //客户端执行的方法
10 //下面的方法是接收并处理服务器方法返回的结果
11 function Success(args,context){
12 message.innerHTML=args;
13 }
14
15 //下面的方式是当接收服务器方法处理的结果发生异常时调用的方法
16 function Error(){
17 message.innerHTML="发生了异常!";
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 //实现ICallbackEventHandler接口
34 {
35
36 String result = String.Empty;
37
38 protected void Page_Load(object sender, EventArgs e)
39 {
40 //获取当前页的ClientScriptManager的引用
41 ClientScriptManager csm = Page.ClientScript;
42 /*获取回调的引用.会在客户端生成WebForm_DoCallback方法,
43 * 调用它来达到异步调用.这个方法是微软写的方法,会被发送
44 到客户端*/
45 /*注意这里的"Success"和Error两个字符串分别是客户端代码中
46 *定义的两个javascript函数*/
47 //下面的方法最后一个参数的意义:true表示执行异步回调,false标志执行同步回调
48 String reference = csm.GetCallbackEventReference(this, "args", "Success", "", "Error", true);
49 String callbackScript = "function CallServerMethod(args,context){\n"+
50 reference+";\n }";
51 //向当前页面注册javascript脚本代码
52 csm.RegisterClientScriptBlock(this.GetType(), "CallServerMethod",callbackScript,true);
53 }
54
55 #region ICallbackEventHandler 成员
56
57 /// <summary>
58 /// 返回回调方法执行结果的方法
59 /// </summary>
60 public string GetCallbackResult()
61 {
62 return result;
63 }
64
65 /// <summary>
66 /// 在服务器端运行回调方法
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技术专栏
2025国考·省考课程试听报名