zoukankan      html  css  js  c++  java
  • 异步回调方法的使用

    来自MSDN:

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Text;

    public partial class newSource_callback : System.Web.UI.Page,ICallbackEventHandler
    {
        public int cbCount = 0;
        //public string cbcount = "";
        public void RaiseCallbackEvent(String eventArgument)
        {
            cbCount = Convert.ToInt32(eventArgument) + 1;
            //cbcount = eventArgument;
        }
        public string GetCallbackResult()
        {
            return cbCount.ToString();
            //return cbcount;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("No page postbacks have occurred.");
            if (Page.IsPostBack)
            {
                sb.Append("A page postback has occurred.");
            }
            MyLabel.Text = sb.ToString();
            ClientScriptManager cs = Page.ClientScript;
            StringBuilder context1 = new StringBuilder();
            context1.Append("function ReceiveServerData1(arg, context){Message1.innerText = arg;value1 = arg;}");
            //context1.Append("{");
            //context1.Append("Message1.innerText = arg;");
            //context1.Append("value1 = arg;");
            //context1.Append("}");
            String cbReference1 = cs.GetCallbackEventReference(this, "arg",
                "ReceiveServerData1", context1.ToString());
            String cbReference2 = cs.GetCallbackEventReference("'" +
                Page.UniqueID + "'", "arg", "ReceiveServerData2", "",
                "ProcessCallBackError", false);
            String callbackScript1 = "function CallTheServer1(arg, context) {" +
                cbReference1 + "; }";
            String callbackScript2 = "function CallTheServer2(arg, context) {" +
                cbReference2 + "; }";
            cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer",
                callbackScript1, true);
            cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer2",
                callbackScript2, true);

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

    <!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>
        <script type="text/javascript">
        var value1 = 0;
        var value2 = 0;
        function ReceiveServerData2(arg, context)
        {
            Message2.innerText = arg;
            value2 = arg;
        }
        function ProcessCallBackError(arg, context)
        {
            Message2.innerText = 'An error has occurred.';
        }
    </script>

    </head>
    <body>
        <form id="form1" runat="server">
        <div>
          Callback 1 result: <span id="Message1">0</span>
          <br />
          Callback 2 result: <span id="Message2">0</span>
          <br /> <br />
          <input type="button"
                 value="ClientCallBack1"
                 onclick="CallTheServer1(value1, alert('Increment value'))"/>   
          <input type="button"
                 value="ClientCallBack2"
                 onclick="CallTheServer2(value2, alert('Increment value'))"/>
          <br /> <br />
          <asp:Label id="MyLabel"
                     runat="server"></asp:Label>
        </div>

        </form>
    </body>
    </html>

  • 相关阅读:
    rsync+inotify实现实时同步,自动触发同步文件
    Linux下实现Rsync目录同步备份
    零基础学python-16.2 作用域法则
    零基础学python-16.1 作用域快速入门
    零基础学python-15.4 函数的多态vs对象的多态
    零基础学python-15.3 函数的定义、调用与多态
    零基础学python-15.2 分解函数
    零基础学python-15.1 为什么需要编写函数
    零基础学python-14.3 python的文档资源:help函数
    零基础学python-14.2 python的文档资源:文档字符串
  • 原文地址:https://www.cnblogs.com/xinzhuangzi/p/4100696.html
Copyright © 2011-2022 走看看