Implementing IScriptControl in UserConttrol

P

Pedro Ferreira

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

Pedro Ferreira

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 said:
your clientid is probably blank.

-- bruce (sqlwork.com)


Pedro Ferreira said:
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
 
P

Pedro Ferreira

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 said:
your clientid is probably blank.

-- bruce (sqlwork.com)


Pedro Ferreira said:
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
 
Top