I have a custom Asp.net control as
public class ImageControl : Panel
{
private RadAsyncUpload AsyncUpload;
}
Multiple ImageControls on the page should use local instances of JS objects so I wrap them in object (closure):
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);
}
http://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
?: I get error MyControls.ImageControl is not a constructor
?: Will it be possible to assign these “packed” functions as event handlers as
AsyncUpload.OnClientFileSelected = "FileSelected";
In “AJAX Server Control Project” Custom class gets inherited from ScriptControl, can I still use high-level wrap “Panel” instead?
Any advise would be appreciated.
step 1.
properly add IScriptControl Interface as shown here: [1] http://vincexu.blogspot.com/2010/02/aspnet-ajax-scriptcontrol-tutorial.html
Now JS part getting loaded.
step 2.
passing var to JS>
this.AsyncUpload = null; // in MyControls.ImageControl = function () { }setting it in code-behind.. in GetScriptDescriptors()
step 3.
create a delegate in prototype:
and add execute it when DOM is ready:
where
The point is assigning EventHandlers to child controls should be done after they are initialized.
======================CS===
======================JS===
The End.