<%@ 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>无标题页</title> <script type="text/javascript"> //向服务器传递参数 function DoSearch(){ var firstName=document.getElementById("TextBox1").value; CallServer(firstName,""); } //得到服务器的数据 function ReceiveServerData(txtUserInfo){ Results.innerHTML=txtUserInfo; } //设置每1秒执行一次 setInterval("DoSearch()",1000); </script> </head> <body> <form runat="server"> <div> 姓名:<asp:TextBox runat="server"></asp:TextBox> <br /> <span></span> </div> </form> </body> </html> [/code] .aspx.cs [code] using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page, ICallbackEventHandler { protected string txtUserInfo; protected void Page_Load(object sender, EventArgs e) { //获取一个对客户端函数的引用 string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context"); //动态注册回调函数 string callbackScript = "function CallServer(arg,context)" + "{" + cbReference + "};"; //引发callbackScript Page.ClientScript.RegisterStartupScript(this.GetType(), "CallServer", callbackScript, true); } //引发Callback事件处理 public void RaiseCallbackEvent(string txtFirstName) { if (txtFirstName != null) { String connString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlserver2008"].ToString(); SqlConnection conn = new SqlConnection(connString); conn.Open(); SqlCommand comm = new SqlCommand("select * from zzx where [name]=@name", conn); comm.Parameters.Add("@name", SqlDbType.VarChar).Value = txtFirstName; SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection); if (reader.Read()) { txtUserInfo = "员工编号:" + reader["id"].ToString() + "<br>"; txtUserInfo += "员工姓名:" + reader["name"].ToString() + "<br>"; txtUserInfo += "地址:" + reader["address"].ToString() + "<br>"; txtUserInfo += "服务器查询时间:" + DateTime.Now.ToString(); } else { if (string.IsNullOrEmpty(txtFirstName)) { txtUserInfo = "请输入姓名"; } else { txtUserInfo = "查无此人"; } } comm.Dispose(); reader.Dispose(); conn.Dispose(); } } //得到回调的结果,返回给客户端 public string GetCallbackResult() { return txtUserInfo; } }
简化版(偷懒一下):
<%@ 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>无标题页</title> <script type="text/javascript"> function OnCallBack(txtUserInfo,context){ Results.innerHTML=txtUserInfo; } </script> </head> <body> <form runat="server"> <div> 姓名:<asp:TextBox runat="server"></asp:TextBox> <input type="button" value="button"document.getElementById('TextBox1').value", "OnCallBack",null)%>" /> <br /> <span></span> </div> </form> </body> </html> .aspx.cs using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Data.SqlClient; using System.Text; public partial class _Default : System.Web.UI.Page, ICallbackEventHandler { protected StringBuilder txtUserInfo; protected void Page_Load(object sender, EventArgs e) { } public string GetCallbackResult() { return txtUserInfo.ToString(); } public void RaiseCallbackEvent(string txtFirstName) { txtUserInfo = new StringBuilder(); String connString = ConfigurationManager.ConnectionStrings["sqlserver2008"].ToString(); SqlConnection conn = new SqlConnection(connString); conn.Open(); SqlCommand comm = new SqlCommand("select * from zzx where [name]=@name", conn); comm.Parameters.Add("@name", SqlDbType.VarChar).Value = txtFirstName; SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection); if (reader.Read()) { txtUserInfo.Append("员工编号:" + reader["id"].ToString() + "<br>"); txtUserInfo.Append("员工姓名:" + reader["name"].ToString() + "<br>"); txtUserInfo.Append("地址:" + reader["address"].ToString() + "<br>"); txtUserInfo.Append("查询时间:" + DateTime.Now.ToString()); } else { if (txtFirstName == string.Empty) { txtUserInfo.Append("请输入姓名"); } else { txtUserInfo.Append("查无此人"); } reader.Dispose(); comm.Dispose(); conn.Dispose(); } } }
示例3: