zoukankan      html  css  js  c++  java
  • ICallbackEventHandler:回调函数的实现(备忘)

    示例来自:ASP.NET 2.0编程珠玑

    服务器端:

    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.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    //继承ICallbackEventHandler,确保使用客户回调特性时,回调事件在会送过程中处理

    public partial class Chapter4_Demo4 : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
    {
        protected void Page_Load(object sender, EventArgs e)
        {

       //检查用户的浏览器是否支持回调特性
            if (!Request.Browser.SupportsCallback)
            {
                throw new ApplicationException("This browser doesn't support Client callbacks.");
            }

       //获取回调事件的引用

            string src = Page.ClientScript.GetCallbackEventReference(this, "arg", "ClientCallback", "ctx", "ClientErrorCallback", false);
            string mainSrc = @"function ValidateNumber(arg,ctx)
                                       { " + src + ";}";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ValidateNumber", mainSrc, true);
        }

        private string _returnFormServer;

      //eventArgs表示客户端方法中的数据

        public void RaiseCallbackEvent(string eventArgs)
        {
            try
            {
                int value = Int32.Parse(eventArgs);

                if (value >= 1 && value <= 1000)
                {
                    this._returnFormServer = "You entered the number: " + eventArgs;
                }
                else
                {
                    this._returnFormServer = "Please enter a number between 1 and 1000";
                }
            }
            catch
            {
                throw new ApplicationException("You must enter a number.");
            }
        }

     //给客户端返回一个字符串,返回值作为一个参数传送给客户端的回调方法

        public string GetCallbackResult()
        {
            return this._returnFormServer;
        }
    }

    客户端:

    <html>
    <head runat="server">
        <title>Untitled Page</title>
        <script language=javascript>
            function Validate()
            {
                var n = document.forms[0].txtNumber.value;
                ValidateNumber(n,"txtNumber");
            }
           
            function ClientCallback(result,context)
            {
                alert(result);
                alert(context);
            }
           
            function ClientErrorCallback(error,context)
            {
                alert("The validation failed. " + error);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            Please enter a number between 1 and 1000:<br />
            <input id="txtNumber" name="txtNumber" type=text />
            <button id="btnValidate" onclick="Validate()">Validate</button>
        </div>
        </form>
    </body>
    </html>

    作者:冰碟
    出处:http://www.cnblogs.com/icebutterfly/
    版权:本文版权归作者和博客园共有
    转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
    要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
  • 相关阅读:
    python---模块与包
    python---迭代器与生成器
    python---装饰器
    Python---函数
    Python---文件操作
    Python---数据类型
    浅谈UBUNTU
    java 键盘输入多种方法
    动态规划解最长公共子序列问题
    线段树
  • 原文地址:https://www.cnblogs.com/icebutterfly/p/1395057.html
Copyright © 2011-2022 走看看