首页 > 学院 > 开发设计 > 正文

Asp.net回调技术Callback学习

2019-11-17 01:40:07
字体:
来源:转载
供稿:网友

asp.net回调技术Callback学习

.aspx:

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

.aspx.cs

C#代码收藏代码
  1. usingSystem;
  2. usingSystem.Collections;
  3. usingSystem.Configuration;
  4. usingSystem.Data;
  5. usingSystem.Web;
  6. usingSystem.Web.Security;
  7. usingSystem.Web.UI;
  8. usingSystem.Web.UI.HtmlControls;
  9. usingSystem.Web.UI.WebControls;
  10. usingSystem.Web.UI.WebControls.WebParts;
  11. usingSystem.Data.SqlClient;
  12. publicpartialclass_Default:System.Web.UI.Page,ICallbackEventHandler
  13. {
  14. PRotectedstringtxtUserInfo;
  15. protectedvoidPage_Load(objectsender,EventArgse)
  16. {
  17. //获取一个对客户端函数的引用
  18. stringcbReference=Page.ClientScript.GetCallbackEventReference(this,"arg","ReceiveServerData","context");
  19. //动态注册回调函数
  20. stringcallbackScript="functionCallServer(arg,context)"+"{"+cbReference+"};";
  21. //引发callbackScript
  22. Page.ClientScript.RegisterStartupScript(this.GetType(),"CallServer",callbackScript,true);
  23. }
  24. //引发Callback事件处理
  25. publicvoidRaiseCallbackEvent(stringtxtFirstName)
  26. {
  27. if(txtFirstName!=null)
  28. {
  29. StringconnString=System.Configuration.ConfigurationManager.ConnectionStrings["sqlserver2008"].ToString();
  30. SqlConnectionconn=newSqlConnection(connString);
  31. conn.Open();
  32. SqlCommandcomm=newSqlCommand("select*fromzzxwhere[name]=@name",conn);
  33. comm.Parameters.Add("@name",SqlDbType.VarChar).Value=txtFirstName;
  34. SqlDataReaderreader=comm.ExecuteReader(CommandBehavior.CloseConnection);
  35. if(reader.Read())
  36. {
  37. txtUserInfo="员工编号:"+reader["id"].ToString()+"<br>";
  38. txtUserInfo+="员工姓名:"+reader["name"].ToString()+"<br>";
  39. txtUserInfo+="地址:"+reader["address"].ToString()+"<br>";
  40. txtUserInfo+="服务器查询时间:"+DateTime.Now.ToString();
  41. }
  42. else
  43. {
  44. if(string.IsNullOrEmpty(txtFirstName))
  45. {
  46. txtUserInfo="请输入姓名";
  47. }
  48. else
  49. {
  50. txtUserInfo="查无此人";
  51. }
  52. }
  53. comm.Dispose();
  54. reader.Dispose();
  55. conn.Dispose();
  56. }
  57. }
  58. //得到回调的结果,返回给客户端
  59. publicstringGetCallbackResult()
  60. {
  61. returntxtUserInfo;
  62. }
  63. }

简化版(偷懒一下):

Html代码收藏代码
  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
  2. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <htmlxmlns="http://www.w3.org/1999/xhtml">
  4. <headrunat="server">
  5. <title>无标题页</title>
  6. <scripttype="text/Javascript">
  7. functionOnCallBack(txtUserInfo,context){
  8. Results.innerHTML=txtUserInfo;
  9. }
  10. </script>
  11. </head>
  12. <body>
  13. <formid="form1"runat="server">
  14. <div>
  15. 姓名:<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>
  16. <inputid="Button2"type="button"value="button"
  17. onclick="<%=Page.ClientScript.GetCallbackEventReference(this,"document.getElementById('TextBox1').value","OnCallBack",null)%>"/>
  18. <br/>
  19. <spanid="Results"style="pink;width:500;"></span>
  20. </div>
  21. </form>
  22. </body>
  23. </html>

.aspx.cs

C#代码收藏代码
  1. usingSystem;
  2. usingSystem.Collections;
  3. usingSystem.Configuration;
  4. usingSystem.Data;
  5. usingSystem.Web;
  6. usingSystem.Web.Security;
  7. usingSystem.Web.UI;
  8. usingSystem.Web.UI.HtmlControls;
  9. usingSystem.Web.UI.WebControls;
  10. usingSystem.Web.UI.WebControls.WebParts;
  11. usingSystem.Data.SqlClient;
  12. usingSystem.Text;
  13. publicpartialclass_Default:System.Web.UI.Page,ICallbackEventHandler
  14. {
  15. protectedStringBuildertxtUserInfo;
  16. protectedvoidPage_Load(objectsender,EventArgse)
  17. {
  18. }
  19. publicstringGetCallbackResult()
  20. {
  21. returntxtUserInfo.ToString();
  22. }
  23. publicvoidRaiseCallbackEvent(stringtxtFirstName)
  24. {
  25. txtUserInfo=newStringBuilder();
  26. StringconnString=ConfigurationManager.ConnectionStrings["sqlserver2008"].ToString();
  27. SqlConnectionconn=newSqlConnection(connString);
  28. conn.Open();
  29. SqlCommandcomm=newSqlCommand("select*fromzzxwhere[name]=@name",conn);
  30. comm.Parameters.Add("@name",SqlDbType.VarChar).Value=txtFirstName;
  31. SqlDataReaderreader=comm.ExecuteReader(CommandBehavior.CloseConnection);
  32. if(reader.Read())
  33. {
  34. txtUserInfo.Append("员工编号:"+reader["id"].ToString()+"<br>");
  35. txtUserInfo.Append("员工姓名:"+reader["name"].ToString()+"<br>");
  36. txtUserInfo.Append("地址:"+reader["address"].ToString()+"<br>");
  37. txtUserInfo.Append("查询时间:"+DateTime.Now.ToString());
  38. }
  39. else
  40. {
  41. if(txtFirstName==string.Empty)
  42. {
  43. txtUserInfo.Append("请输入姓名");
  44. }
  45. else
  46. {
  47. txtUserInfo.Append("查无此人");
  48. }
  49. reader.Dispose();
  50. comm.Dispose();
  51. conn.Dispose();
  52. }
  53. }
  54. }

示例3:

Html代码收藏代码
  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default3.aspx.cs"Inherits="Default3"%>
  2. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <htmlxmlns="http://www.w3.org/1999/xhtml">
  4. <headrunat="server">
  5. <title>无标题页</title>
  6. <scripttype="text/javascript">
  7. //客户端执行的方法
  8. //下面的方法是接收并处理服务器方法返回的结果
  9. functionSuccess(args,context){
  10. message.innerHTML=args;
  11. }
  12. //下面的方式是当接收服务器方法处理的结果发生异常时调用的方法
  13. functionError(){
  14. message.innerHTML="发生了异常!";
  15. }
  16. </script>
  17. </head>
  18. <body>
  19. <formid="form1"runat="server">
  20. <div>
  21. 用户名:<inputtype="text"id="txtUserName"onblur="CallServerMethod(txtUserName.value,nu
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表