• ASP.NET2.0中的Callback机制


          要解析callback的实现机制,那还是让我们从一个callback调用的客户端脚本调用开始,看看,微软是怎么实现这个callback机制的。

      1、ClientScript.GetCallbackEventReference(...)

      要激发一个callback,首先,当然需要在客户端本中发出一个调用。一个典型的调用语法如下:

    <script language="javascript" type="text/javascript">
    function any_script_function(arg, context)
    {
     <%= ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context")%>;
    }
    </script> 

      ClientScript.GetCallbackEventReference(...)将根据传入的参数返回实际的回调脚本。这个函数有多个重载版本,因此,这些参数的含义,大家可以参考MSDN。以具体的上面这段示例代码中的参数来说:

      - this表示执行回调的的服务端控件是当前这个Page,当前的Page必须实现ICallbackEventHandler接口,包括必须实现string GetCallbackResult()和void RaiseCallbackEvent(eventArgument)这两个接口函数,这个参数也可以是指向某个WEB控件的引用,当然,这个空间也必须实现ICallbackEventHandler接口;

      - "arg"是将被传给RaiseCallbackEvent的参数eventArgument的值,可以使人以自定义格式的字符串;

      - "ReceiveServerData"是当回调成功之后,处理返回内容的客户端脚本函数的名称,这个函数必须存在于执行回调的页面,并且这个函数可以包含两个参数,例如:

    <script type="text/javascript">
    function ReceiveServerData(result, context)
    {}
    </script> 

      这两个参数,分别是回调的返回数据result,和原封不动被返回的我们激发回调时的这个context参数,当然,这两个参数都是字符串类型的。

    CS代码如下:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    public partial class third : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
    {
       private string returnValue;
      protected void Page_Load(object sender, EventArgs e)
      {   
          /string str = Page.ClientScript.GetCallbackEventReference(this, "document.getElementById('Select1').options[document.getElementById('Select1').selectedIndex].text", "ResultValue", null);
          ////return false是为了防止提交窗体
          //Button1.Attributes.Add("onclick", str + ";return false;");
           }

      #region ICallbackEventHandler Members
      public void RaiseCallbackEvent(string eventArgument)
      {
       if (eventArgument == "KKKKK")
       {
        returnValue= "KKKKKKKKK!";
       }
       else
       {
        returnValue= "BTBTBTBTBT!";
       }
      }
        public string GetCallbackResult()
        {
            return returnValue;
        }
      #endregion
    }

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

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
    <form id="form2" runat="server">
    <div>
    <select id="Select1">
    <option selected="selected" value=1>KKKKK</option>
    <option value=2>BTBTBTBT</option>
    </select>
    <asp:Button ID="Button1" runat="server" Text="这是个服务器按钮" /></div>
    <div id="div1" />
    <script type="text/javascript">
    function ResultValue(ret,context)
    {
      document.getElementById("div1").innerHTML = ret;
      alert(ret);
    }
    </script>
    </form>
    </body>
    </html>

  • 相关阅读:
    部分数据文件损坏的修复处理示例.sql
    使用UPDATE进行编号重排的处理示例.sql
    DNS Prefetching 技术引入及实现方法
    查找指定节点的所有子节点的示例函数.sql
    特殊的交叉报表处理示例.sql
    定时备份FTP+Mysql到云服务器
    cPanel下安装GodaddySSL教程
    移动节点处理的通用存储过程.sql
    应用程序角色使用示例.sql
    文件及文件组备份与还原示例.sql
  • 原文地址:https://www.cnblogs.com/jony/p/398159.html
Copyright © 2020-2023  润新知