前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >asp.net回调javascript

asp.net回调javascript

作者头像
阿新
发布2018-04-09 18:01:31
7200
发布2018-04-09 18:01:31
举报
文章被收录于专栏:c#开发者
代码语言: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(); 
        } 
 } 
} 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2005-09-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档