zoukankan      html  css  js  c++  java
  • Asp.net回调技术Callback学习

    .aspx:

    Html代码  收藏代码
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
    2.   
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    4. <html xmlns="http://www.w3.org/1999/xhtml">  
    5. <head runat="server">  
    6.     <title>无标题页</title>  
    7.   
    8.     <script type="text/javascript">  
    9.        
    10.      //向服务器传递参数  
    11.      function DoSearch(){  
    12.        var firstName=document.getElementById("TextBox1").value;  
    13.         CallServer(firstName,"");  
    14.      }  
    15.        
    16.      //得到服务器的数据  
    17.      function ReceiveServerData(txtUserInfo){  
    18.         Results.innerHTML=txtUserInfo;  
    19.      }  
    20.        
    21.      //设置每1秒执行一次  
    22.      setInterval("DoSearch()",1000);  
    23.     </script>  
    24.   
    25. </head>  
    26. <body>  
    27.     <form id="form1" runat="server">  
    28.     <div>  
    29.         姓名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
    30.         <br />  
    31.         <span id="Results" style=" 500px;"></span>  
    32.     </div>  
    33.     </form>  
    34. </body>  
    35. </html>  

      

      .aspx.cs

    C#代码  收藏代码
    1. using System;  
    2. using System.Collections;  
    3. using System.Configuration;  
    4. using System.Data;  
    5. using System.Web;  
    6. using System.Web.Security;  
    7. using System.Web.UI;  
    8. using System.Web.UI.HtmlControls;  
    9. using System.Web.UI.WebControls;  
    10. using System.Web.UI.WebControls.WebParts;  
    11. using System.Data.SqlClient;  
    12.   
    13. public partial class _Default : System.Web.UI.Page, ICallbackEventHandler  
    14. {  
    15.     protected string txtUserInfo;  
    16.   
    17.   
    18.     protected void Page_Load(object sender, EventArgs e)  
    19.     {  
    20.         //获取一个对客户端函数的引用  
    21.         string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");  
    22.         //动态注册回调函数  
    23.         string callbackScript = "function CallServer(arg,context)" + "{" + cbReference + "};";  
    24.         //引发callbackScript  
    25.         Page.ClientScript.RegisterStartupScript(this.GetType(), "CallServer", callbackScript, true);  
    26.     }  
    27.   
    28.     //引发Callback事件处理  
    29.     public void RaiseCallbackEvent(string txtFirstName)  
    30.     {  
    31.         if (txtFirstName != null)  
    32.         {  
    33.             String connString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlserver2008"].ToString();  
    34.   
    35.             SqlConnection conn = new SqlConnection(connString);  
    36.   
    37.             conn.Open();  
    38.   
    39.             SqlCommand comm = new SqlCommand("select * from zzx where [name]=@name", conn);  
    40.   
    41.             comm.Parameters.Add("@name", SqlDbType.VarChar).Value = txtFirstName;  
    42.   
    43.             SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);  
    44.             if (reader.Read())  
    45.             {  
    46.                 txtUserInfo = "员工编号:" + reader["id"].ToString() + "<br>";  
    47.                 txtUserInfo += "员工姓名:" + reader["name"].ToString() + "<br>";  
    48.                 txtUserInfo += "地址:" + reader["address"].ToString() + "<br>";  
    49.                 txtUserInfo += "服务器查询时间:" + DateTime.Now.ToString();  
    50.             }  
    51.             else  
    52.             {  
    53.                 if (string.IsNullOrEmpty(txtFirstName))  
    54.                 {  
    55.                     txtUserInfo = "请输入姓名";  
    56.                 }  
    57.                 else  
    58.                 {  
    59.                     txtUserInfo = "查无此人";  
    60.                 }  
    61.             }  
    62.   
    63.             comm.Dispose();  
    64.             reader.Dispose();  
    65.             conn.Dispose();  
    66.         }  
    67.     }  
    68.   
    69.     //得到回调的结果,返回给客户端  
    70.     public string GetCallbackResult()  
    71.     {  
    72.         return txtUserInfo;  
    73.     }  
    74.   
    75.   
    76. }  

    简化版(偷懒一下):

    Html代码  收藏代码
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
    2.   
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    4. <html xmlns="http://www.w3.org/1999/xhtml">  
    5. <head runat="server">  
    6.     <title>无标题页</title>  
    7.   
    8.     <script type="text/javascript">  
    9.     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;  500;"></span>  
    23.     </div>  
    24.     </form>  
    25. </body>  
    26. </html>  

     .aspx.cs

    C#代码  收藏代码
    1. using System;  
    2. using System.Collections;  
    3. using System.Configuration;  
    4. using System.Data;  
    5. using System.Web;  
    6. using System.Web.Security;  
    7. using System.Web.UI;  
    8. using System.Web.UI.HtmlControls;  
    9. using System.Web.UI.WebControls;  
    10. using System.Web.UI.WebControls.WebParts;  
    11. using System.Data.SqlClient;  
    12. using System.Text;  
    13. public partial class _Default : System.Web.UI.Page, ICallbackEventHandler  
    14. {  
    15.     protected StringBuilder txtUserInfo;  
    16.   
    17.     protected void Page_Load(object sender, EventArgs e)  
    18.     {  
    19.   
    20.     }  
    21.   
    22.     public string GetCallbackResult()  
    23.     {  
    24.         return txtUserInfo.ToString();  
    25.     }  
    26.   
    27.     public void RaiseCallbackEvent(string txtFirstName)  
    28.     {  
    29.         txtUserInfo = new StringBuilder();  
    30.         String connString = ConfigurationManager.ConnectionStrings["sqlserver2008"].ToString();  
    31.         SqlConnection conn = new SqlConnection(connString);  
    32.         conn.Open();  
    33.         SqlCommand comm = new SqlCommand("select * from zzx where [name]=@name", conn);  
    34.         comm.Parameters.Add("@name", SqlDbType.VarChar).Value = txtFirstName;  
    35.         SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);  
    36.         if (reader.Read())  
    37.         {  
    38.             txtUserInfo.Append("员工编号:" + reader["id"].ToString() + "<br>");  
    39.             txtUserInfo.Append("员工姓名:" + reader["name"].ToString() + "<br>");  
    40.             txtUserInfo.Append("地址:" + reader["address"].ToString() + "<br>");  
    41.             txtUserInfo.Append("查询时间:" + DateTime.Now.ToString());  
    42.         }  
    43.         else  
    44.         {  
    45.             if (txtFirstName == string.Empty)  
    46.             {  
    47.                 txtUserInfo.Append("请输入姓名");  
    48.             }  
    49.             else  
    50.             {  
    51.                 txtUserInfo.Append("查无此人");  
    52.             }  
    53.             reader.Dispose();  
    54.             comm.Dispose();  
    55.             conn.Dispose();  
    56.         }  
    57.   
    58.   
    59.     }  
    60. }  

       示例3:

    Html代码  收藏代码
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>  
    2.   
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    4.   
    5. <html xmlns="http://www.w3.org/1999/xhtml">  
    6. <head runat="server">  
    7.     <title>无标题页</title>  
    8.     <script type="text/javascript">  
    9.     //客户端执行的方法  
    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>  

      

    C#代码  收藏代码
    1. public partial class Default3 : System.Web.UI.Page,ICallbackEventHandler //实现ICallbackEventHandler接口  
    2. {  
    3.   
    4.     String result = String.Empty;  
    5.   
    6.     protected void Page_Load(object sender, EventArgs e)  
    7.     {  
    8.         //获取当前页的ClientScriptManager的引用  
    9.         ClientScriptManager csm = Page.ClientScript;  
    10.         /*获取回调的引用.会在客户端生成WebForm_DoCallback方法, 
    11.          * 调用它来达到异步调用.这个方法是微软写的方法,会被发送 
    12.          到客户端*/  
    13.         /*注意这里的"Success"和Error两个字符串分别是客户端代码中 
    14.          *定义的两个javascript函数*/  
    15.         //下面的方法最后一个参数的意义:true表示执行异步回调,false标志执行同步回调  
    16.         String reference = csm.GetCallbackEventReference(this, "args", "Success", "", "Error", true);  
    17.         String callbackScript = "function CallServerMethod(args,context){ "+  
    18.             reference+";  }";  
    19.         //向当前页面注册javascript脚本代码  
    20.         csm.RegisterClientScriptBlock(this.GetType(), "CallServerMethod",callbackScript,true);  
    21.     }  
    22.  
    23.     #region ICallbackEventHandler 成员  
    24.   
    25.     /// <summary>  
    26.     /// 返回回调方法执行结果的方法  
    27.     /// </summary>  
    28.     public string GetCallbackResult()  
    29.     {  
    30.         return result;  
    31.     }  
    32.   
    33.     /// <summary>  
    34.     /// 在服务器端运行回调方法  
    35.     /// </summary>  
    36.     public void RaiseCallbackEvent(string eventArgument)  
    37.     {  
    38.         if (eventArgument.ToLower().IndexOf("admin")!=-1)  
    39.         {  
    40.             result =eventArgument+ "不能作为用户注册.";  
    41.         }  
    42.         else  
    43.         {  
    44.             result = eventArgument + "可以注册.";  
    45.         }  
    46.     }  
    47.  
    48.     #endregion  
    49. }  
  • 相关阅读:
    uva 10474 Where is the Marble?(简单题)
    【cocos2d-x 3.7 飞机大战】 决战南海I (二) 我方飞机的实现
    STM32中assert_param的使用
    OC语言--内存管理
    php通过shell调用Hadoop的方法
    前端JSON使用总结
    Google地图接口API之Google地图 API 参考手册(七)
    Google地图接口API之地图类型(六)
    Google地图接口API之地图控件集(五)
    Google地图接口API之地图事件(四)
  • 原文地址:https://www.cnblogs.com/xiaochao12345/p/3884140.html
Copyright © 2011-2022 走看看