P
Patrick Ruzand
Hi,
I have written a very simple custom control that renders itself as a DIV.
This custom control implements the System.Web.Extensions.IScriptControl
interface.
Here is an extract of the server code:
public partial class MyControl : UserControl, IScriptControl
{
public override void RenderControl(HtmlTextWriter writer) {
// Some style attributes are set here
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.RenderEndTag();
base.RenderControl(writer);
}
protected override void OnPreRender(EventArgs e) {
if (!this.DesignMode) {
// Test for ScriptManager and register if it exists
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 sm = ScriptManager.GetCurrent(Page);
if (sm == null)
throw new HttpException("A ScriptManager control must exist
on the current page.");
sm.RegisterScriptDescriptors(this);
}
base.Render(writer);
}
public System.Collections.Generic.IEnumerable<ScriptDescriptor>
GetScriptDescriptors() {
ScriptControlDescriptor descriptor = new
ScriptControlDescriptor("Sample.MyControl", this.ID);
return new ScriptDescriptor[] { descriptor };
}
public System.Collections.Generic.IEnumerable<ScriptReference>
GetScriptReferences() {
ScriptReference reference =
new ScriptReference("mycontrol.js");
return new ScriptReference[] { reference };
}
}
On the client side, this control has a corresponding javascript class.
In the initialize method, I add a dummy handler on the 'keydown' event
that pops an alert message.
Here is the corresponding javascript code:
Type.registerNamespace("Sample");
Sample.MyControl = function(element) {
Sample.MyControl.initializeBase(this, [element]);
this._keydownHandler = null;
}
Sample.MyControl.prototype = {
initialize : function() {
Sample.MyControl.callBaseMethod(this, 'initialize');
this._keydownHandler = Function.createDelegate(this,
this._onkeydown);
$addHandler(this.get_element(), 'keydown', this._keydownHandler);
},
dispose : function() {
var elt = this.get_element();
if (elt) {
$removeHandler(elt, 'keydown', this._keydownHandler);
}
this._keydownHandler = null;
Sample.MyControl.callBaseMethod(this, 'dispose');
},
_onkeydown : function(e) {
alert('onkeydown');
}
}
Sample.MyControl.registerClass('Sample.MyControl', Sys.UI.Control);
if (typeof(Sys) !== 'undefined')
Sys.Application.notifyScriptLoaded();
My problem is while it works well under IE, it does not work in Firefox.
I may surely have done a silly mistake, but I can't find it til now...
Let me know if you need more information.
Thanks for your help,
I have written a very simple custom control that renders itself as a DIV.
This custom control implements the System.Web.Extensions.IScriptControl
interface.
Here is an extract of the server code:
public partial class MyControl : UserControl, IScriptControl
{
public override void RenderControl(HtmlTextWriter writer) {
// Some style attributes are set here
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.RenderEndTag();
base.RenderControl(writer);
}
protected override void OnPreRender(EventArgs e) {
if (!this.DesignMode) {
// Test for ScriptManager and register if it exists
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 sm = ScriptManager.GetCurrent(Page);
if (sm == null)
throw new HttpException("A ScriptManager control must exist
on the current page.");
sm.RegisterScriptDescriptors(this);
}
base.Render(writer);
}
public System.Collections.Generic.IEnumerable<ScriptDescriptor>
GetScriptDescriptors() {
ScriptControlDescriptor descriptor = new
ScriptControlDescriptor("Sample.MyControl", this.ID);
return new ScriptDescriptor[] { descriptor };
}
public System.Collections.Generic.IEnumerable<ScriptReference>
GetScriptReferences() {
ScriptReference reference =
new ScriptReference("mycontrol.js");
return new ScriptReference[] { reference };
}
}
On the client side, this control has a corresponding javascript class.
In the initialize method, I add a dummy handler on the 'keydown' event
that pops an alert message.
Here is the corresponding javascript code:
Type.registerNamespace("Sample");
Sample.MyControl = function(element) {
Sample.MyControl.initializeBase(this, [element]);
this._keydownHandler = null;
}
Sample.MyControl.prototype = {
initialize : function() {
Sample.MyControl.callBaseMethod(this, 'initialize');
this._keydownHandler = Function.createDelegate(this,
this._onkeydown);
$addHandler(this.get_element(), 'keydown', this._keydownHandler);
},
dispose : function() {
var elt = this.get_element();
if (elt) {
$removeHandler(elt, 'keydown', this._keydownHandler);
}
this._keydownHandler = null;
Sample.MyControl.callBaseMethod(this, 'dispose');
},
_onkeydown : function(e) {
alert('onkeydown');
}
}
Sample.MyControl.registerClass('Sample.MyControl', Sys.UI.Control);
if (typeof(Sys) !== 'undefined')
Sys.Application.notifyScriptLoaded();
My problem is while it works well under IE, it does not work in Firefox.
I may surely have done a silly mistake, but I can't find it til now...
Let me know if you need more information.
Thanks for your help,