[英]Javascript Closure for C# Asp.net Custom Control
我有一個自定義的Asp.net控件
public class ImageControl : Panel
{
private RadAsyncUpload AsyncUpload;
}
頁面上的多個ImageControl應該使用JS對象的本地實例,因此我將它們包裝在object中(關閉):
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繼承的,我是否仍可以使用高級包裝“面板”來代替?
任何建議將不勝感激。
步驟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();
結束。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.