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