473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Raising events in a control

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(functio nName,"Just Entered
function");
System.Web.HttpContext.Current.Trace.Write(functio nName,"Just Exiting
Function");
}

public virtual bool LoadPostData(string postDataKey, NameValueCollection
postCollection)
{
string functionName = "LoadPostData";
System.Web.HttpContext.Current.Trace.Write(functio nName,"Just Entered
function");
System.Web.HttpContext.Current.Trace.Write(functio nName,"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 !!!!*****");
}
}
Nov 19 '05 #1
1 1533
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

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: The Mess | last post by:
I am working in VB5. How do you mimmick Visual Basic's putting an Index argument at the front of the variables passed to an event when making your own control? This sounds kind of confusing so...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
4
by: rawCoder | last post by:
Hi all, How Can You Raise Events Asynchronously ? Now for the details ... I want to do inter modular communication using events in such a way that the contributing modules need not...
7
by: Colin Young | last post by:
I have a UserControl that contains a calendar control. The calendar is not raising events (month navigation, date selections, etc.). I've checked that the OnSelectionChanged event has a handler...
0
by: Greg Park | last post by:
I have many user controls loading into a web page using Page.LoadControl However, I'm unable to figure out how to raise an event when a button is click or a check box is checked. I can have...
1
by: Wade | last post by:
Hi all, I am dynamically adding a link button to a page, but for some reason it is not raising the click event in the code behind. Here are some snippets from my code -- any help is greatly...
5
by: snesbit | last post by:
If a screen is made up of several user controls and those user controls contain various packaged or standard controls such as a grid, how do you raise both standard and custom events from the user...
4
by: Dave A | last post by:
I am developing a somewhat complex component at the moment and coincidently I am also reading the Framework Design Guidelines book. After reading the section about event raising I have re-written...
2
by: Gman | last post by:
Hi, I have created a usercontrol, a grid control essentially. Within it I have a class: clsGridRecord. I have coded the events such that when a user clicks on the grid, say, the events occur on...
7
by: Christian Cambier | last post by:
Hi, I have a textbox in a web user control. I then add the usercontrol to a web form. I also add a label in the web form. Now, when I press a key in the textbox, i want to display some text...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.