Asp.net回调技术Callback学习笔记

.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

 
 
 
无标题页 

 

 
 
姓名:
[/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() + "
"; txtUserInfo += "员工姓名:" + reader["name"].ToString() + "
"; txtUserInfo += "地址:" + reader["address"].ToString() + "
"; 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" %> 

 
 
 
无标题页 

 

 
 
姓名: " />
.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() + "
"); txtUserInfo.Append("员工姓名:" + reader["name"].ToString() + "
"); txtUserInfo.Append("地址:" + reader["address"].ToString() + "
"); txtUserInfo.Append("查询时间:" + DateTime.Now.ToString()); } else { if (txtFirstName == string.Empty) { txtUserInfo.Append("请输入姓名"); } else { txtUserInfo.Append("查无此人"); } reader.Dispose(); comm.Dispose(); conn.Dispose(); } } }

示例3:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> 

 

 
 
无标题页 
 
 
 
用户名:
密码:
[code] public partial class Default3 : System.Web.UI.Page,ICallbackEventHandler //实现ICallbackEventHandler接口 { String result = String.Empty; protected void Page_Load(object sender, EventArgs e) { //获取当前页的ClientScriptManager的引用 ClientScriptManager csm = Page.ClientScript; /*获取回调的引用.会在客户端生成WebForm_DoCallback方法, * 调用它来达到异步调用.这个方法是微软写的方法,会被发送 到客户端*/ /*注意这里的"Success"和Error两个字符串分别是客户端代码中 *定义的两个javascript函数*/ //下面的方法最后一个参数的意义:true表示执行异步回调,false标志执行同步回调 String reference = csm.GetCallbackEventReference(this, "args", "Success", "", "Error", true); String callbackScript = "function CallServerMethod(args,context){\n"+ reference+";\n }"; //向当前页面注册javascript脚本代码 csm.RegisterClientScriptBlock(this.GetType(), "CallServerMethod",callbackScript,true); } #region ICallbackEventHandler 成员 /// /// 返回回调方法执行结果的方法 /// public string GetCallbackResult() { return result; } /// /// 在服务器端运行回调方法 /// public void RaiseCallbackEvent(string eventArgument) { if (eventArgument.ToLower().IndexOf("admin")!=-1) { result =eventArgument+ "不能作为用户注册."; } else { result = eventArgument + "可以注册."; } } #endregion }

你可能感兴趣的:(Asp.net回调技术Callback学习笔记)