【问题标题】:Javascript Closure for C# Asp.net Custom ControlC# Asp.net 自定义控件的 Javascript 闭包
【发布时间】:2012-03-27 08:42:02
【问题描述】:

我有一个自定义的 Asp.net 控件

public class ImageControl : Panel
{
        private RadAsyncUpload AsyncUpload;
}

页面上的多个 ImageControls 应该使用 JS 对象的本地实例,所以我将它们包装在对象中(闭包):

JS>

Type.registerNamespace("MyControls.ImageControl");

MyControls.ImageControl = function () {   
    this._filesUploaded = 0;
    this._maxFileCount = 1;
};

MyControls.ImageControl.prototype = {
 inc_filesUploaded: function () {
        this._filesUploaded++;
    },
 FileSelected: function (sender, args) {
        inc_filesUploaded();        
    }
};
MyControls.ImageControl.registerClass('MyControls.ImageControl');

ASP.NET>

protected override void Render(HtmlTextWriter writer)
{
    Page.ClientScript.RegisterClientScriptResource(_TYPE, JS.ImageControl);
    writer.Write(@"
<script type=""text/javascript"" id=""" + ClientID + @"ScriptHost"">
(function( ) {
    var onLoad = function( ) {
        window." + ID + @" = new MyControls.ImageControl();
    };
    if (window.addEventListener) {
        window.addEventListener('load', onLoad, false);
    }
    else if (window.attachEvent) {
        window.attachEvent('onload', onLoad);
    }
})( );
</script>
");
    base.Render(writer);
}

www.asp.net/AJAX/Documentation/Live/tutorials/CreatingCustomClientControlsTutorial.aspx

stackoverflow.com/questions/6309947/javascript-closure-advantages

http://www.codeproject.com/Articles/55414/From-Simple-JavaScript-Classes-to-ASP-NET-AJAX-Con

http://www.netfxharmonics.com/2008/11/Creating-JavaScript-Components-and-ASPNET-Controls

?: 我得到错误 MyControls.ImageControl 不是构造函数 ?: 是否可以将这些“打包”函数分配为事件处理程序

AsyncUpload.OnClientFileSelected = "FileSelected";

在“AJAX 服务器控件项目”中,自定义类从 ScriptControl 继承,我还可以使用高级包装“面板”吗?

任何建议将不胜感激。

【问题讨论】:

  • 好的。第一步是正确添加 IScriptControl 接口,如下所示:vincexu.blogspot.com/2010/02/… 现在 JS 部分正在加载,但我仍然需要弄清楚如何将处理程序添加到 AsyncUpload
  • 第二步是将变量传递给 JS> this.idx = null; this.AsyncUpload = null;并在代码隐藏中设置它们.. descriptor.AddProperty("idx", this.ClientID); descriptor.AddProperty("AsyncUpload", this.AsyncUpload.C​​lientID);

标签: c# javascript asp.net controls closures


【解决方案1】:

第 1 步。 正确添加 IScriptControl 接口,如下所示:[1] http://vincexu.blogspot.com/2010/02/aspnet-ajax-scriptcontrol-tutorial.html 现在 JS 部分正在加载。

第 2 步。 将 var 传递给 JS>

this.AsyncUpload = null; // in MyControls.ImageControl = function () { }

在 GetScriptDescriptors() 中的代码隐藏中设置它

descriptor.AddProperty("AsyncUpload", this.AsyncUpload.ClientID);

第 3 步。 在原型中创建一个委托:

this._FileSelected = Function.createDelegate(this, this.FileSelected);

并在 DOM 准备好时添加执行它:

this.addLoadEvent(this._FileSelected);

在哪里

addLoadEvent: function(func) {
  var oldonload = window.onload;
if (typeof window.onload != 'function') {
    window.onload = func;
} else {
    window.onload = function() {
        if (oldonload) {
            oldonload();
        }
        func();
    }

重点是应该在初始化子控件之后将 EventHandlers 分配给子控件。

=======================CS===

public class ServerControl1 : Panel, IScriptControl
    {           
        private RadAsyncUpload AsyncUpload;

        public ServerControl1()
        {
            ID = Guid.NewGuid().ToString();
            AsyncUpload = new RadAsyncUpload();
        }
        protected override void OnInit(EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptResource(GetType(), "ImageControl.jquery.min.js");                
            Controls.Add(AsyncUpload);
            base.OnInit(e);
        }
        protected override void OnPreRender(EventArgs e)
        {
            var manager = ScriptManager.GetCurrent(Page);
            if (manager == null)
            {
                throw new InvalidOperationException("A ScriptManager is required on the page.");
            }
            manager.RegisterScriptControl(this);
            base.OnPreRender(e);
        }
        protected override void Render(HtmlTextWriter writer)
        {
            if (!DesignMode)
                ScriptManager.GetCurrent(Page).RegisterScriptDescriptors(this);
            base.Render(writer);
        }    
        public IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            var descriptor = new ScriptControlDescriptor("ImageControl.ClientControl1", this.ClientID);
            descriptor.AddProperty("AsyncUpload", this.AsyncUpload.ClientID);
            yield return descriptor;
        }    
        public IEnumerable<ScriptReference> GetScriptReferences()
        {
            yield return new ScriptReference("ImageControl.ClientControl1.js", GetType().Assembly.FullName);
        }
    }

=======================JS===

Type.registerNamespace("ImageControl");    
ImageControl.ClientControl1 = function (element) {
    ImageControl.ClientControl1.initializeBase(this, [element]);
    this.AsyncUpload = null;
}    
ImageControl.ClientControl1.prototype = {
    initialize: function () {
        ImageControl.ClientControl1.callBaseMethod(this, 'initialize');            
        this._Added = Function.createDelegate(this, this.Added);
        this._initAsyncClientFunctions = Function.createDelegate(this, this.initAsyncClientFunctions);            
        this.addLoadEvent(this._initAsyncClientFunctions);            
    },
    dispose: function () {
        ImageControl.ClientControl1.callBaseMethod(this, 'dispose');            
    },    
    addLoadEvent: function(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function() {
                if (oldonload) {
                    oldonload();
                }
                func();
            }
        }
    },    
    initAsyncClientFunctions: function()
    {            
        var asyncUpload = $find(this.AsyncUpload);
        asyncUpload.add_added(this._Added);        
    },    
    Added: function () {            
        alert('added ' + this.AsyncUpload);        
    },
};    
ImageControl.ClientControl1.registerClass('ImageControl.ClientControl1', Sys.UI.Control);    
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

结局。

【讨论】:

    猜你喜欢
    • 2011-03-01
    • 2011-03-23
    • 2010-12-08
    • 2010-09-23
    • 2011-06-15
    • 2011-12-20
    • 2012-06-07
    • 1970-01-01
    相关资源
    最近更新 更多