Implementing IScriptControl in UserConttrol

Discussion in 'ASP .Net' started by Pedro Ferreira, Feb 25, 2008.

  1. Hi,

    Is it possible to implement the IScriptControl in a UserControl?

    I keep getting the following error: "Sys.ArgumentException: Value must not
    be null for Controls and Behaviors. Parameter name: element"

    I have no problems implementing it in a server control, but can't get it to
    work on user controls. Any ideas of what I'm doing wrong?

    Here's the sample code I'm using:

    <%@ Control Language="C#" AutoEventWireup="true"
    Inherits="SampleScriptControl.MyUserControl" %>
    <asp:Label runat="server" ID="label1" Text="MyUserControl" />

    public partial class MyUserControl : UserControl, IScriptControl
    protected override void OnPreRender(EventArgs e)
    ScriptManager sm = ScriptManager.GetCurrent(Page);

    if(sm == null)
    throw new HttpException("A ScriptManager control must exist on the
    current page.");



    protected override void Render(HtmlTextWriter writer)


    public System.Collections.Generic.IEnumerable<ScriptDescriptor>
    ScriptControlDescriptor sd = new
    ScriptControlDescriptor("SampleScriptControl.MyUserControl", this.ClientID);

    sd.AddProperty("labelID", this.label1.ClientID);

    yield return sd;

    public System.Collections.Generic.IEnumerable<ScriptReference>
    ScriptReference sr = new ScriptReference("~/MyUserControl.js");

    yield return sr;


    SampleScriptControl.MyUserControl = function(element)
    SampleScriptControl.MyUserControl.initializeBase(this, [element]);

    this._labelID = null;

    SampleScriptControl.MyUserControl.prototype =
    initialize: function()
    SampleScriptControl.MyUserControl.callBaseMethod(this, 'initialize');

    dispose: function()
    SampleScriptControl.MyUserControl.callBaseMethod(this, 'dispose');

    get_labelID : function()
    return this._labelID;

    set_labelID : function(value)
    this._labelID = value;

    SampleScriptControl.MyUserControl.registerClass('SampleScriptControl.MyUserControl', Sys.UI.Control);

    if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
    Pedro Ferreira, Feb 25, 2008
    1. Advertisements

  2. Pedro Ferreira

    bruce barker Guest

    your clientid is probably blank.

    -- bruce (
    bruce barker, Feb 25, 2008
    1. Advertisements

  3. Hello Bruce, thanks for your help.

    Yes, I thought that it could be the case. But as far as I can see the
    ClientID has a valid string ("MyUserControl" in this case). I've used a
    breakpoint in the GetScriptDescriptors method to check this.

    I'm thinking this may be a simple problem, maybe a typo, or something I'm
    repeatadely doing wrong.

    Any help would be appreciated.


    Pedro Ferreira, Feb 25, 2008
  4. Ok, got some more info.

    I'm getting the same error even in server controls.

    The only way of not getting the error, is to use CompositeControl as a base
    class of my control. If I use the ScriptControl as base class the error

    So it must be something related to the moment the ClientID property is used.
    Does this make any sense?

    In user controls, I cannot use CompositeControl so I'll have to figure
    what's happening.



    Pedro Ferreira, Feb 25, 2008
  5. Pedro Ferreira

    Ewert Bergh

    Dec 12, 2014
    Likes Received:
    Hi I had the same problem when trying to implement IScriptControl on a Webusercontrol. The problem is that Webusercontrol's only act as containers and don't generate their own ClientID's. A way to solve this is to add a "dummy" control to point to. Add a Panel control in your user control and reference it's ID in your implementation of the GetScriptDescriptors() function. e.g. ScriptControlDescriptor descriptor = new ScriptControlDescriptor("Control.Namespace.ControlType", this.pnlPanelMain.ClientID);
    Ewert Bergh, Dec 12, 2014
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.