Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7728129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:38:24+00:00 2026-06-01T05:38:24+00:00

I have a custom Asp.net control as public class ImageControl : Panel { private

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:38:25+00:00Added an answer on June 1, 2026 at 5:38 am

    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()

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

    step 3.
    create a delegate in prototype:

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

    and add execute it when DOM is ready:

    this.addLoadEvent(this._FileSelected);
    

    where

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

    The point is assigning EventHandlers to child controls should be done after they are initialized.

    ======================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();
    

    The End.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom asp.net server control to display images.What I need now is
I have a custom asp-net control that inherits from another one and its works
I have a custom control that extends ASP.NET Update panels. On the server side
I have a custom ASP.NET control. In its Init handler I add a delegate
I have a custom ASP.NET server control CustomControl with a property attribute Path .
Basically I want to be able to have a custom control: public class MyControl
I have an asp.net custom server control. Is there anyway I can reuse it
I have created a simple Asp.Net custom control which automatically combines all the correct
I have a custom control which is rendered as a hyperlink: Public Class TestControl
So i have a ASP.NET 4 Custom Control called SafeClickButton which is designed to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.