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();
     
    Pedro Ferreira, Feb 25, 2008
    #1
    1. Advertisements

  2. Pedro Ferreira

    bruce barker Guest

    your clientid is probably blank.

    -- bruce (sqlwork.com)
     
    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


     
    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

     
    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

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.