微软的CallBack机制(入个门)

学习文档:http://download.csdn.net/source/899736

实例一:

前台

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckName.aspx.cs" Inherits="CallBack_CheckName" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>判断用户名是否被占用</title>
  6.     <script type="text/javascript">
  7.         function CallServer(inputcontrol,context)
  8.         {
  9.            context.innerHTML = "加载中......";
  10.            arg = inputcontrol.value;
  11.            <%= ClientScript.GetCallbackEventReference(this, "arg", "OnCallBack", null) %>
  12.         }
  13.         function OnCallBack(result,context)
  14.         {
  15.            //context.innerHTML = ""; //当引用为null时,这里不可用.
  16.            lblShow.innerHTML = "";   
  17.             alert(result);
  18.         }
  19.     </script>
  20. </head>
  21. <body>
  22.     <form id="form1" runat="server">
  23.     <div>
  24.         <asp:textbox ID="txtEnter" runat="server"></asp:textbox><br />
  25.         <input id="btnSubmit" type="button" value="submit" onclick="CallServer(txtEnter,lblShow);" /><br />
  26.         <asp:label ID="lblShow" runat="server" Text=""></asp:label>
  27.     </div>
  28.     </form>
  29. </body>
  30. </html>

后台

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class CallBack_CheckName : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
  12. {
  13.     private string result;
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.     }
  17.     //引发回调事件处理  
  18.     public void RaiseCallbackEvent(string eventArgument)
  19.     {
  20.         if (eventArgument == "greatverve")
  21.             result = "被占用,请改一个!";
  22.         else
  23.             result = "可以使用!";
  24.     }
  25.     //回传回调结果
  26.     public string GetCallbackResult()
  27.     {
  28.         return result;
  29.     }
  30. }

实例二:

前台

  1. <%@ page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CallBack_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5.     <title>客户端回调</title>
  6.     <script type="text/javascript">
  7.         function CallServer(inputcontrol,context)
  8.         {
  9.          //回调还没有处理完全时其预先加载的显示值
  10.          context.innerHTML = "加载中......";
  11.          //为你在文本框中输入的信息,并且arg在这里就是将其值传递到
  12.          //RaiseCallbackEvent(String eventArgument)方法对应的eventArgument中
  13.          arg = inputcontrol.value;
  14.          //获取一个对客户端函数的引用;调用该函数时,将启动一个对服务器端事件的客户端回调。
  15.          //第一个参数:表示当前页;第二个参数:发向服务器的string;
  16.          //第三个参数:回调函数;  第四个参数:
  17.         <%= ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context") %>
  18.         }
  19.         //这个是回调的函数
  20.         //这个函数只是提供必要的参数.真正的回调是在服务器端执行.
  21.         //格式固定,第一个参数是返回的数据,第二个参数是引用的id
  22.         function ReceiveServerData(result,context)
  23.         {
  24.             context.innerHTML = result;
  25.             lbl_show.innerHTML = result;
  26.             alert(result);
  27.         }
  28.         //如果传入的参数是服务控件的id,就可以不用getElementByID();
  29.         //非服务器控件也可以直接取,呵呵.
  30.         function f(serverControl){
  31.             alert(serverControl.value);
  32.         }
  33.     </script>
  34. </head>
  35. <body>
  36.     <form id="form1" runat="server">
  37.         <div>
  38.             <asp:textbox ID="txtEnter" runat="server"></asp:textbox><br />
  39.             <input id="btnSubmit" type="button" value="submit" onclick="CallServer(txtEnter,lblShow);" /><br />
  40.             <asp:label ID="lblShow" runat="server" Text="Label"></asp:label>
  41.         </div>
  42.         <div>
  43.             <asp:label ID="lbl_show" runat="server" Text="Label"></asp:label>            
  44.             <input type="button" value="取得服务器控件" onclick="f(txtEnter);" /><br />
  45.             <input id="txtInput" type="text" /><br />
  46.             <input type="button" value="submit" onclick="f(txtInput);" />
  47.         </div>
  48.     </form>
  49. </body>
  50. </html>

后台

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class CallBack_Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
  12. {
  13.     //定义一个字符串,回调的结果信息将保存在该字符串中
  14.     private string result;
  15.     protected void Page_Load(object sender, EventArgs e)
  16.     {
  17.     }
  18.     //引发回调事件处理  
  19.     public void RaiseCallbackEvent(string eventArgument)
  20.     {
  21.         //"eventArgument"为从客户端的JavaScript传递过来的参数
  22.         result = "从服务器端返回的内容:" + eventArgument;
  23.     }
  24.     //回传回调结果
  25.     public string GetCallbackResult()
  26.     {
  27.         return result;
  28.     }
  29. }

end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值