• asp.net回调javascript


    using System;
    using System.Collections;
    using System.IO;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;

    namespace Platform
    {
       
       
     /// <summary>
     /// 可以处理客户端的jscript的回调
     /// </summary>
     [DefaultProperty(""),
      ToolboxData("<{0}:ScriptCallbackManager runat=server></{0}:ScriptCallbackManager>")]
     public class ScriptCallbackManager : System.Web.UI.WebControls.WebControl
     {
      private ICallbackEventHandler _handler;
      private String _functionNameAfterCallback;

            public ScriptCallbackManager() {
                //_handler = (ICallbackEventHandler)Page;

            }

        


            /// <summary>
            /// 回调完执行的客户端函数
            /// </summary>
      public string FunctionNameAfterCallback
      {
       get { return _functionNameAfterCallback; }
                set { _functionNameAfterCallback = value; }
      }

      public ICallbackEventHandler CallbackEventHandler
      {
       get { return _handler; }
                set { _handler = value; }

      }

      /// <summary>
      /// 将此控件呈现给指定的输出参数。
      /// </summary>
      /// <param name="output"> 要写出到的 HTML 编写器 </param>
      protected override void Render(HtmlTextWriter output)
      {
       output.Write(this.GetCallbackEventReference());
      }

      protected override void OnInit(EventArgs e){
       base.OnInit(e);
       try {
        this.HandleRequest();
                }
       catch (Exception ex){
                    object o = ex;
       }
      }

      private void HandleRequest(){
       HttpRequest request = this.Page.Request;
       HttpResponse reponse = this.Page.Response;
       String sType = request["type"];
       if ((sType != null) && (sType == "__scriptcallback:" + this.ID))
       {
        String sRequest = this.GetRequestContent(request);
        String sResult = this.CallHandler(sRequest);
        reponse.Clear();
        reponse.Expires = 0;
        reponse.Write(sResult);
        reponse.End();
       }
      }

      private String GetRequestContent(HttpRequest request) {
       HttpRequest r = request;
       String s = r.ToString();
       using (StreamReader reader = new StreamReader(request.InputStream)) {
        return reader.ReadToEnd();
       }
      }

            private String CallHandler(String param) {
                if (this.CallbackEventHandler != null) {
                    return this.CallbackEventHandler.RaiseCallbackEvent(this, param);
                }
                return "";
            }

      /*
      private ICallbackEventHandler GetScriptCallbackHandlerOnPage(String id)
      {
       return this.GetScriptCallbackHandler(this.Page, id);
      }

      private ICallbackEventHandler GetScriptCallbackHandler(Control control, String id)
      {
       if ((control is ICallbackEventHandler) && (control.ID == id))
       {
        return control as ICallbackEventHandler;
       }
       foreach(Control child in control.Controls)
       {
        ICallbackEventHandler handler = GetScriptCallbackHandler(child, id);
        if (handler != null)
        {
         return handler;
        }
       }
       return null;
      }
      */

            private String GetCallbackEventReference()
            {
             StringBuilder sb = new StringBuilder();
       sb.Append("<script>");
       sb.Append("function " + this.ID + "(param, context) {");
       sb.Append("var xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');");
       sb.Append("xmlhttp.Open('POST', '" + HttpContext.Current.Request.Url.ToString() + "?type=__scriptcallback:" + this.ID + "', false);");
       sb.Append("xmlhttp.Send(param);");
       sb.Append(this.FunctionNameAfterCallback + "(xmlhttp.responseText, context);");
       sb.Append("}");
       sb.Append("</script>");

       return sb.ToString();
            }

     }
    }

  • 相关阅读:
    hdu 1174
    计算几何模板
    又是一年博客记
    hdu 1225 Football Score
    与逆序数有关的
    hdu 2844 Coins
    hdu 1171 Big Event in HDU
    UVA Exponentiation
    UVA Summation of Four Primes
    Linux:设置alias永久生效
  • 原文地址:https://www.cnblogs.com/neozhu/p/233043.html
Copyright © 2020-2023  润新知