Raising events in a control

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}:postBackControl runat=server></{0}:postBackControl>")]
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 !!!!*****");
}
}
 
G

garethdjames

I guess this is what is going on,

when you raise the even you must first check if the delegate is null,
it will be null if you have not assigned (added) an event handler in
your page class

so..
if(MyCustomEvent != null)
{
MyCustomEvent(sender,e); ////ERROR ALWAYS RAISED -- OBJECT REFERECE
NOT
}


in the page class (which has your control on)

private void InitializeComponent()
{
//other code here
this.MyControl.MyCustomEvent += new EventHandler(c_MyCustomEvent);
}

You get a null pointer because there is no method wired up to respond
to the event in te page
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top