C#Asp.net自定义控件的Javascript闭包

Javascript Closure for C# Asp.net Custom Control

本文关键字:Javascript 闭包 自定义控件 net C#Asp      更新时间:2023-09-26

我有一个自定义的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/AAJX/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继承,我还可以使用高级包装"面板"吗?

如有任何建议,不胜感激。

步骤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();

结束。