473,789 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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" .
"MyCustomEv ent" is raise in the event "btn_Click" (at bottom of email)

However I ALWAYS get an error on the line

MyCustomEvent(s ender,e);
saying "object reference not set to an instance of an object"

it is the object "MyCustomEv ent" 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 "MyCustomEv ent" to check if it is null or not, but then my custom
event "MyCustomEv ent" 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}:postBackCont rol runat=server></{0}:postBackCon trol>")]
public class postBackControl : System.Web.UI.W ebControls.WebC ontrol,
IPostBackDataHa ndler, INamingContaine r
{
public event EventHandler MyCustomEvent; //CUSTOM EVENT DECLARED
Button btn;

protected override void OnInit(EventArg s e)
{
base.OnInit(e);
if (Page != null)
Page.RegisterRe quiresPostBack( this);
}

protected override void CreateChildCont rols()
{
btn = new Button();
btn.Text = "TestButton ";
btn.Click += new EventHandler(bt n_Click);
this.Controls.A dd(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(HtmlText Writer output)
{
btn.RenderContr ol(output);
}

public virtual void RaisePostDataCh angedEvent()
{
string functionName = "RaisePostDataC hangedEvent";
System.Web.Http Context.Current .Trace.Write(fu nctionName,"Jus t Entered
function");
System.Web.Http Context.Current .Trace.Write(fu nctionName,"Jus t Exiting
Function");
}

public virtual bool LoadPostData(st ring postDataKey, NameValueCollec tion
postCollection)
{
string functionName = "LoadPostDa ta";
System.Web.Http Context.Current .Trace.Write(fu nctionName,"Jus t Entered
function");
System.Web.Http Context.Current .Trace.Write(fu nctionName,"Jus t Exiting
Function");
return true;
}

private void btn_Click(objec t sender, EventArgs e)
{
System.Web.Http Context.Current .Response.Write ("******GOT THE Button Click
from btn_Click !!!!*****<br>") ;
MyCustomEvent(s ender,e); ////ERROR ALWAYS RAISED -- OBJECT REFERECE NOT
SET TO AN INSTANCE OF AN OBJECT.
System.Web.Http Context.Current .Trace.Write("* *****GOT THE Button Click
from btn_Click !!!!*****");
}
}
Nov 19 '05 #1
1 1546
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(MyCustomEven t != null)
{
MyCustomEvent(s ender,e); ////ERROR ALWAYS RAISED -- OBJECT REFERECE
NOT
}
in the page class (which has your control on)

private void InitializeCompo nent()
{
//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
4426
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 I'll simplify it. Say I have made my own UserControl and I have an event for it. Someone uses my control. In fact he makes a control array of it. When an event is fired, how do you tell for which instance of the control. Could someone give me an...
6
2885
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 TimerState object similar to the example in the System.Threading.Timer documentation. The method which handles the timer events, among other things, periodically calls a method in this TimerState object which raises an event to the startup form,...
4
17270
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 maintain the reference to any module.
7
2691
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 being registered. Is there anything else obvious or otherwise that I've missed? Is there a problem using it in a UserControl? Thanks Colin
0
1724
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 upto a hundred user controls on a single page and AutoPostBack isn't an option. I have no problem with raising events when an user control is dropped on the aspx page.
1
1285
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 appreciated: ' declaring variable Protected WithEvents lbtEquipment As System.Web.UI.WebControls.LinkButton
5
1737
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 control so the form containing the user control can see the events you want them to see. Help Please....
4
1599
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 the way my component raises events to follow the Framework Design Guides verbatim; ie (object sender, EventArgs (or some subclass there of) e). The thing that was not explained is why should I need to cast the sender to 'object' when I know...
2
1698
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 the parent form. This is fine. The problem occurs when I want to raise an event for a user clicking on one of the clsRecords which are on the grid. So I've placed: Public Event GridRecordClicked(ByVal rec As clsGridRecord,
7
2874
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 in the label. I tried by raising events as used in Winforms but it is not as easy as
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9506
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9016
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6761
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.