Asp.net回调技术Callback学习
作者:佚名    ASP.NET网站开发编辑:admin   更新时间:2022-07-23
asp.net回调技术Callback学习
.aspx:
Html代码
- <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <headrunat="server">
- <title>无标题页</title>
- <scripttype="text/javascript">
- //向服务器传递参数
- functionDoSearch(){
- varfirstName=document.getElementById("TextBox1").value;
- CallServer(firstName,"");
- }
- //得到服务器的数据
- functionReceiveServerData(txtUserInfo){
- Results.innerHTML=txtUserInfo;
- }
- //设置每1秒执行一次
- setInterval("DoSearch()",1000);
- </script>
- </head>
- <body>
- <formid="form1"runat="server">
- <div>
- 姓名:<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>
- <br/>
- <spanid="Results"style="width:500px;"></span>
- </div>
- </form>
- </body>
- </html>
.aspx.cs
C#代码
- usingSystem;
- usingSystem.Collections;
- usingSystem.Configuration;
- usingSystem.Data;
- usingSystem.Web;
- usingSystem.Web.Security;
- usingSystem.Web.UI;
- usingSystem.Web.UI.HtmlControls;
- usingSystem.Web.UI.WebControls;
- usingSystem.Web.UI.WebControls.WebParts;
- usingSystem.Data.SqlClient;
- publicpartialclass_Default:System.Web.UI.Page,ICallbackEventHandler
- {
- PRotectedstringtxtUserInfo;
- protectedvoidPage_Load(objectsender,EventArgse)
- {
- //获取一个对客户端函数的引用
- stringcbReference=Page.ClientScript.GetCallbackEventReference(this,"arg","ReceiveServerData","context");
- //动态注册回调函数
- stringcallbackScript="functionCallServer(arg,context)"+"{"+cbReference+"};";
- //引发callbackScript
- Page.ClientScript.RegisterStartupScript(this.GetType(),"CallServer",callbackScript,true);
- }
- //引发Callback事件处理
- publicvoidRaiseCallbackEvent(stringtxtFirstName)
- {
- if(txtFirstName!=null)
- {
- StringconnString=System.Configuration.ConfigurationManager.ConnectionStrings["sqlserver2008"].ToString();
- SqlConnectionconn=newSqlConnection(connString);
- conn.Open();
- SqlCommandcomm=newSqlCommand("select*fromzzxwhere[name]=@name",conn);
- comm.Parameters.Add("@name",SqlDbType.VarChar).Value=txtFirstName;
- SqlDataReaderreader=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();
- }
- }
- //得到回调的结果,返回给客户端
- publicstringGetCallbackResult()
- {
- returntxtUserInfo;
- }
- }
简化版(偷懒一下):
Html代码
- <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <headrunat="server">
- <title>无标题页</title>
- <scripttype="text/Javascript">
- functionOnCallBack(txtUserInfo,context){
- Results.innerHTML=txtUserInfo;
- }
- </script>
- </head>
- <body>
- <formid="form1"runat="server">
- <div>
- 姓名:<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>
- <inputid="Button2"type="button"value="button"
- onclick="<%=Page.ClientScript.GetCallbackEventReference(this,"document.getElementById('TextBox1').value","OnCallBack",null)%>"/>
- <br/>
- <spanid="Results"style="pink;width:500;"></span>
- </div>
- </form>
- </body>
- </html>
.aspx.cs
C#代码
- usingSystem;
- usingSystem.Collections;
- usingSystem.Configuration;
- usingSystem.Data;
- usingSystem.Web;
- usingSystem.Web.Security;
- usingSystem.Web.UI;
- usingSystem.Web.UI.HtmlControls;
- usingSystem.Web.UI.WebControls;
- usingSystem.Web.UI.WebControls.WebParts;
- usingSystem.Data.SqlClient;
- usingSystem.Text;
- publicpartialclass_Default:System.Web.UI.Page,ICallbackEventHandler
- {
- protectedStringBuildertxtUserInfo;
- protectedvoidPage_Load(objectsender,EventArgse)
- {
- }
- publicstringGetCallbackResult()
- {
- returntxtUserInfo.ToString();
- }
- publicvoidRaiseCallbackEvent(stringtxtFirstName)
- {
- txtUserInfo=newStringBuilder();
- StringconnString=ConfigurationManager.ConnectionStrings["sqlserver2008"].ToString();
- SqlConnectionconn=newSqlConnection(connString);
- conn.Open();
- SqlCommandcomm=newSqlCommand("select*fromzzxwhere[name]=@name",conn);
- comm.Parameters.Add("@name",SqlDbType.VarChar).Value=txtFirstName;
- SqlDataReaderreader=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:
Html代码
- <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default3.aspx.cs"Inherits="Default3"%>
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- <headrunat="server">
- <title>无标题页</title>
- <scripttype="text/javascript">
- //客户端执行的方法
- //下面的方法是接收并处理服务器方法返回的结果
- functionSuccess(args,context){
- message.innerHTML=args;
- }
- //下面的方式是当接收服务器方法处理的结果发生异常时调用的方法
- functionError(){
- message.innerHTML="发生了异常!";
- }
- </script>
- </head>
- <body>
- <formid="form1"runat="server">
- <div>
- 用户名:<inputtype="text"id="txtUserName"onblur="CallServerMethod(txtUserName.value,nu