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:

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

    --MyUserControl.ascx.cs--
    public partial class MyUserControl : UserControl, IScriptControl
    {
    protected override void OnPreRender(EventArgs e)
    {
    if(!this.DesignMode)
    {
    ScriptManager sm = ScriptManager.GetCurrent(Page);

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

    sm.RegisterScriptControl(this);
    }

    base.OnPreRender(e);
    }

    protected override void Render(HtmlTextWriter writer)
    {
    if(!this.DesignMode)
    ScriptManager.GetCurrent(Page).RegisterScriptDescriptors(this);

    base.Render(writer);
    }

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

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

    yield return sd;
    }

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

    yield return sr;
    }
    }

    --MyUserControl.js--
    Type.registerNamespace("SampleScriptControl");

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

    ---

    Thanks,

    Pedro Ferreira
     
    Pedro Ferreira, Feb 25, 2008
    #1
    1. Advertisements

  2. Pedro Ferreira

    bruce barker Guest

    your clientid is probably blank.

    -- bruce (sqlwork.com)


    "Pedro Ferreira" wrote:

    > 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:
    >
    > --MyUserControl.ascx--
    > <%@ Control Language="C#" AutoEventWireup="true"
    > CodeBehind="MyUserControl.ascx.cs"
    > Inherits="SampleScriptControl.MyUserControl" %>
    > <asp:Label runat="server" ID="label1" Text="MyUserControl" />
    >
    > --MyUserControl.ascx.cs--
    > public partial class MyUserControl : UserControl, IScriptControl
    > {
    > protected override void OnPreRender(EventArgs e)
    > {
    > if(!this.DesignMode)
    > {
    > ScriptManager sm = ScriptManager.GetCurrent(Page);
    >
    > if(sm == null)
    > throw new HttpException("A ScriptManager control must exist on the
    > current page.");
    >
    > sm.RegisterScriptControl(this);
    > }
    >
    > base.OnPreRender(e);
    > }
    >
    > protected override void Render(HtmlTextWriter writer)
    > {
    > if(!this.DesignMode)
    > ScriptManager.GetCurrent(Page).RegisterScriptDescriptors(this);
    >
    > base.Render(writer);
    > }
    >
    > public System.Collections.Generic.IEnumerable<ScriptDescriptor>
    > GetScriptDescriptors()
    > {
    > ScriptControlDescriptor sd = new
    > ScriptControlDescriptor("SampleScriptControl.MyUserControl", this.ClientID);
    >
    > sd.AddProperty("labelID", this.label1.ClientID);
    >
    > yield return sd;
    > }
    >
    > public System.Collections.Generic.IEnumerable<ScriptReference>
    > GetScriptReferences()
    > {
    > ScriptReference sr = new ScriptReference("~/MyUserControl.js");
    >
    > yield return sr;
    > }
    > }
    >
    > --MyUserControl.js--
    > Type.registerNamespace("SampleScriptControl");
    >
    > 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();
    >
    > ---
    >
    > Thanks,
    >
    > Pedro Ferreira
     
    bruce barker, Feb 25, 2008
    #2
    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


    "bruce barker" wrote:

    > your clientid is probably blank.
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "Pedro Ferreira" wrote:
    >
    > > 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:
    > >
    > > --MyUserControl.ascx--
    > > <%@ Control Language="C#" AutoEventWireup="true"
    > > CodeBehind="MyUserControl.ascx.cs"
    > > Inherits="SampleScriptControl.MyUserControl" %>
    > > <asp:Label runat="server" ID="label1" Text="MyUserControl" />
    > >
    > > --MyUserControl.ascx.cs--
    > > public partial class MyUserControl : UserControl, IScriptControl
    > > {
    > > protected override void OnPreRender(EventArgs e)
    > > {
    > > if(!this.DesignMode)
    > > {
    > > ScriptManager sm = ScriptManager.GetCurrent(Page);
    > >
    > > if(sm == null)
    > > throw new HttpException("A ScriptManager control must exist on the
    > > current page.");
    > >
    > > sm.RegisterScriptControl(this);
    > > }
    > >
    > > base.OnPreRender(e);
    > > }
    > >
    > > protected override void Render(HtmlTextWriter writer)
    > > {
    > > if(!this.DesignMode)
    > > ScriptManager.GetCurrent(Page).RegisterScriptDescriptors(this);
    > >
    > > base.Render(writer);
    > > }
    > >
    > > public System.Collections.Generic.IEnumerable<ScriptDescriptor>
    > > GetScriptDescriptors()
    > > {
    > > ScriptControlDescriptor sd = new
    > > ScriptControlDescriptor("SampleScriptControl.MyUserControl", this.ClientID);
    > >
    > > sd.AddProperty("labelID", this.label1.ClientID);
    > >
    > > yield return sd;
    > > }
    > >
    > > public System.Collections.Generic.IEnumerable<ScriptReference>
    > > GetScriptReferences()
    > > {
    > > ScriptReference sr = new ScriptReference("~/MyUserControl.js");
    > >
    > > yield return sr;
    > > }
    > > }
    > >
    > > --MyUserControl.js--
    > > Type.registerNamespace("SampleScriptControl");
    > >
    > > 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();
    > >
    > > ---
    > >
    > > Thanks,
    > >
    > > Pedro Ferreira
     
    Pedro Ferreira, Feb 25, 2008
    #3
  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
    appears.

    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.

    Thanks,

    Pedro

    "bruce barker" wrote:

    > your clientid is probably blank.
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "Pedro Ferreira" wrote:
    >
    > > 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:
    > >
    > > --MyUserControl.ascx--
    > > <%@ Control Language="C#" AutoEventWireup="true"
    > > CodeBehind="MyUserControl.ascx.cs"
    > > Inherits="SampleScriptControl.MyUserControl" %>
    > > <asp:Label runat="server" ID="label1" Text="MyUserControl" />
    > >
    > > --MyUserControl.ascx.cs--
    > > public partial class MyUserControl : UserControl, IScriptControl
    > > {
    > > protected override void OnPreRender(EventArgs e)
    > > {
    > > if(!this.DesignMode)
    > > {
    > > ScriptManager sm = ScriptManager.GetCurrent(Page);
    > >
    > > if(sm == null)
    > > throw new HttpException("A ScriptManager control must exist on the
    > > current page.");
    > >
    > > sm.RegisterScriptControl(this);
    > > }
    > >
    > > base.OnPreRender(e);
    > > }
    > >
    > > protected override void Render(HtmlTextWriter writer)
    > > {
    > > if(!this.DesignMode)
    > > ScriptManager.GetCurrent(Page).RegisterScriptDescriptors(this);
    > >
    > > base.Render(writer);
    > > }
    > >
    > > public System.Collections.Generic.IEnumerable<ScriptDescriptor>
    > > GetScriptDescriptors()
    > > {
    > > ScriptControlDescriptor sd = new
    > > ScriptControlDescriptor("SampleScriptControl.MyUserControl", this.ClientID);
    > >
    > > sd.AddProperty("labelID", this.label1.ClientID);
    > >
    > > yield return sd;
    > > }
    > >
    > > public System.Collections.Generic.IEnumerable<ScriptReference>
    > > GetScriptReferences()
    > > {
    > > ScriptReference sr = new ScriptReference("~/MyUserControl.js");
    > >
    > > yield return sr;
    > > }
    > > }
    > >
    > > --MyUserControl.js--
    > > Type.registerNamespace("SampleScriptControl");
    > >
    > > 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();
    > >
    > > ---
    > >
    > > Thanks,
    > >
    > > Pedro Ferreira
     
    Pedro Ferreira, Feb 25, 2008
    #4
  5. Pedro Ferreira

    Ewert Bergh

    Joined:
    Dec 12, 2014
    Messages:
    1
    Likes Received:
    0
    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
    #5
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gomathi

    Implementing Interface

    Gomathi, Nov 17, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    836
    S.M. Altaf [MVP]
    Nov 17, 2005
  2. Eqbal Z
    Replies:
    3
    Views:
    6,015
    Eqbal
    Nov 16, 2003
  3. Dev

    Implementing E1 - E3

    Dev, Sep 9, 2004, in forum: VHDL
    Replies:
    1
    Views:
    990
    Allan Herriman
    Sep 9, 2004
  4. Johnsy Joseph
    Replies:
    2
    Views:
    1,383
    Brian Drummond
    Oct 29, 2004
  5. Riccardo Fregonese

    Need help implementing a proj on SPARTAN3

    Riccardo Fregonese, Dec 2, 2004, in forum: VHDL
    Replies:
    2
    Views:
    945
    Thomas Gebauer
    Jan 3, 2005
  6. SD
    Replies:
    1
    Views:
    635
  7. Mark

    Implementing a Back button

    Mark, Jun 25, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,201
  8. Patrick Ruzand
    Replies:
    5
    Views:
    592
    Patrick Ruzand
    Mar 15, 2007
Loading...