Custom event not firing
Hello,
I have a problem with custom controls and raising events (asp.net, vs2005, frameowork 2.0).
I am making a simple TestControl to learn how custom controls work.
Most things went fine, but now I ran into a problem that
a custom event is not fired. See code piece below.
I have commented the lines that cause the problem.
Problem: for some reason, the event-declaration is not executed,
so 'MyEvent' is always null, and thus the event is not fired.
But the question is: why is the declaration not executed (why is 'MyEvent' is always null)
and what can I do about it?
Can anybody help me please?
Regards,
Martin
-------
************************************************** **********************
namespace TestControl
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:TestControl runat=server></{0}:TestControl>")]
public class TestControl : WebControl, IPostBackDataHandler, IPostBackEventHandler
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
// ----------------------------------------------
// Declare delegate for event
public delegate void EventDelegate (Object sender, EventArgs e);
public event EventDelegate MyEvent; // *** This line is not executed. why?
protected virtual void OnMyEvent(EventArgs e)
{
if (MyEvent != null) MyEvent(this, e); // *** here, MyEvent is null. Why?
}
// ----------------------------------------------
public void RaisePostBackEvent(String eventArgument)
{
if (eventArgument == "Add")
{
this.Value++;
EventArgs e = new EventArgs();
OnMyEvent(EventArgs.Empty);
}
else
{
this.Value--;
}
}
// ----------------------------------------
protected override void OnInit(EventArgs e)
{
Page.RegisterRequiresPostBack(this);
}
// ---------------------------------------
...
} // end of classs
}
|