M
Martin
Hi,
I have been having trouble rasing events in my custom (composite) server
controls.
To demonstate my problem I put a small control together with a single button
on it and included the code below.
Now what I want is when the button is clicked I wish to catch the event
(this is happening correctly) and then raise the event "MyCustomEvent".
"MyCustomEvent" is raise in the event "btn_Click" (at bottom of email)
However I ALWAYS get an error on the line
MyCustomEvent(sender,e);
saying "object reference not set to an instance of an object"
it is the object "MyCustomEvent" that is apparently not set to a correct
instance but I am not sure how to overcome this error.
It appears to me that I am doing things the way I would do them in asp.net
page but it is not working.
I appreciate that to stop the error message being displayed I could just
check "MyCustomEvent" to check if it is null or not, but then my custom
event "MyCustomEvent" would never get raised as it would always be null.
so my question is "How can I raise a custom event from my composite control
and what additional steps do I have to take to avoid the error described."
I have implemented INaming container and the control appears correctly in
the control tree.
many thanks in advance.
cheers
martin.
===================================================================================
[DefaultProperty("Text"),
ToolboxData("<{0}
ostBackControl runat=server></{0}
ostBackControl>")]
public class postBackControl : System.Web.UI.WebControls.WebControl,
IPostBackDataHandler, INamingContainer
{
public event EventHandler MyCustomEvent; //CUSTOM EVENT DECLARED
Button btn;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Page != null)
Page.RegisterRequiresPostBack(this);
}
protected override void CreateChildControls()
{
btn = new Button();
btn.Text = "TestButton";
btn.Click += new EventHandler(btn_Click);
this.Controls.Add(btn);
}
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
btn.RenderControl(output);
}
public virtual void RaisePostDataChangedEvent()
{
string functionName = "RaisePostDataChangedEvent";
System.Web.HttpContext.Current.Trace.Write(functionName,"Just Entered
function");
System.Web.HttpContext.Current.Trace.Write(functionName,"Just Exiting
Function");
}
public virtual bool LoadPostData(string postDataKey, NameValueCollection
postCollection)
{
string functionName = "LoadPostData";
System.Web.HttpContext.Current.Trace.Write(functionName,"Just Entered
function");
System.Web.HttpContext.Current.Trace.Write(functionName,"Just Exiting
Function");
return true;
}
private void btn_Click(object sender, EventArgs e)
{
System.Web.HttpContext.Current.Response.Write("******GOT THE Button Click
from btn_Click !!!!*****<br>");
MyCustomEvent(sender,e); ////ERROR ALWAYS RAISED -- OBJECT REFERECE NOT
SET TO AN INSTANCE OF AN OBJECT.
System.Web.HttpContext.Current.Trace.Write("******GOT THE Button Click
from btn_Click !!!!*****");
}
}
I have been having trouble rasing events in my custom (composite) server
controls.
To demonstate my problem I put a small control together with a single button
on it and included the code below.
Now what I want is when the button is clicked I wish to catch the event
(this is happening correctly) and then raise the event "MyCustomEvent".
"MyCustomEvent" is raise in the event "btn_Click" (at bottom of email)
However I ALWAYS get an error on the line
MyCustomEvent(sender,e);
saying "object reference not set to an instance of an object"
it is the object "MyCustomEvent" that is apparently not set to a correct
instance but I am not sure how to overcome this error.
It appears to me that I am doing things the way I would do them in asp.net
page but it is not working.
I appreciate that to stop the error message being displayed I could just
check "MyCustomEvent" to check if it is null or not, but then my custom
event "MyCustomEvent" would never get raised as it would always be null.
so my question is "How can I raise a custom event from my composite control
and what additional steps do I have to take to avoid the error described."
I have implemented INaming container and the control appears correctly in
the control tree.
many thanks in advance.
cheers
martin.
===================================================================================
[DefaultProperty("Text"),
ToolboxData("<{0}
public class postBackControl : System.Web.UI.WebControls.WebControl,
IPostBackDataHandler, INamingContainer
{
public event EventHandler MyCustomEvent; //CUSTOM EVENT DECLARED
Button btn;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Page != null)
Page.RegisterRequiresPostBack(this);
}
protected override void CreateChildControls()
{
btn = new Button();
btn.Text = "TestButton";
btn.Click += new EventHandler(btn_Click);
this.Controls.Add(btn);
}
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
btn.RenderControl(output);
}
public virtual void RaisePostDataChangedEvent()
{
string functionName = "RaisePostDataChangedEvent";
System.Web.HttpContext.Current.Trace.Write(functionName,"Just Entered
function");
System.Web.HttpContext.Current.Trace.Write(functionName,"Just Exiting
Function");
}
public virtual bool LoadPostData(string postDataKey, NameValueCollection
postCollection)
{
string functionName = "LoadPostData";
System.Web.HttpContext.Current.Trace.Write(functionName,"Just Entered
function");
System.Web.HttpContext.Current.Trace.Write(functionName,"Just Exiting
Function");
return true;
}
private void btn_Click(object sender, EventArgs e)
{
System.Web.HttpContext.Current.Response.Write("******GOT THE Button Click
from btn_Click !!!!*****<br>");
MyCustomEvent(sender,e); ////ERROR ALWAYS RAISED -- OBJECT REFERECE NOT
SET TO AN INSTANCE OF AN OBJECT.
System.Web.HttpContext.Current.Trace.Write("******GOT THE Button Click
from btn_Click !!!!*****");
}
}