.NET Tip: Creating and Using Your Own Events Boston MA

Want to make managing the sequence of events within a Web page easier? Simply register for a custom event and run your code when the event occurs.

Local Companies

Eze Castle Integration
(617) 217-3000
1 Federal Street, 9th Flr.
Boston, MA
Cogent Communications
(617) 482-5546
1 Summer Street, Ste. 450
Boston, MA
WordStream Internet Marketing
617-457-7870
133 Federal St.
Boston, MA
Applied Technologies, Inc.
(617) 742-2525
One Center Plaza, Ste. 240
Boston, MA
Thomas Reuters
(617) 856-2000
22 Thomson Place
Boston, MA
Starr Center @ The Schepens Eye Research Instittute
(617) 912-0100
185 Cambridge Street, 2nd Floor
Boston, MA
VIPER consulting_inc
(617)6860170
660 Massachusetts Avenue, Suite 6
Boston, MA
JCALPRO
(617) 954-2345
415 Summer Street
Boston, MA
INX Inc.
(617) 728-4440
2 Oliver Street, 10th Floor
Boston, MA
Brattle Consulting Group, Inc.
(617) 229-7210
8 Faneuil Hall Marketplace
Boston, MA

provided by: 
Originally published at Internet.com


When I'm writing code for user controls that need to signal the host page when they're done with some task, I define and use an event. I simply register for a custom event and run my code when the event occurs. This makes a clean interface between the user control and the page and makes managing the sequence of events within a page easier.

An application I have uses a custom Login control that handles user registration and login functions. This control has an event called UserAuthenticated that passes back the user's unique ID number, as well as a flag indicating whether the information should be saved permanently using a cookie or other mechanism.

I start by defining the event and its handler in the LoginControl: public delegate void UserAuthHandler(int userID, bool saveInfo); public event UserAuthHandler UserAuthenticated;

Each event I define has both delegate and event definitions. The first declaration indicates which data can be sent with the event. The second declaration is the actual event. Next, I cause the event to occur: UserAuthenticated(userID, chkSavePassword.Checked);

I call the event just as I would any other function. In this case, I pass the data required by the event. This code will work only if the hosting page or control has registered for the event-otherwise, I'll get an error when I "raise" the event.

The code to register for the event is done in the same way as any other event handler. I can either add the attribute to the server control tag, or add the event handler in the OnInit event. My preference is to do all the event handler linkages within the OnInit event so they are all in one place and easy to find. Here's the code for this particular event: protected override void OnInit(EventArgs e) { base.OnInit(e); ctlLogin.UserAuthenticated += new LoginControl.UserAuthHandler(ctlLogin_UserAuthenticated); }

As with other events, typing this line creates the following function in the Web page: protected void ctlLogin_UserAuthenticated(int userID, bool saveInfo) { }

In my page, the login control is hidden and the next panel is displayed when the login is complete. I also store the user ID for later use in a cookie that persists based on the value of the saveInfo parameter.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic. Send him your questions and feedback via e-mail at questions@techniquescentral.com.

Author: Eric Smith

Read article at Internet.com site

Featured Local Company

Eze Castle Integration

(617) 217-3000
1 Federal Street, 9th Flr.
Boston, MA

Related Local Events
Software Development Best Practices 2009
Dates: 9/21/2009 - 9/24/2009
Location: Hynes Convention Center
Boston, MA
View Details

EMBEDDED SYSTEMS CONFERENCE - BOSTON 2009
Dates: 9/21/2009 - 9/24/2009
Location: Hynes Convention Center
Boston, MA
View Details

September Networking Breakfast
Dates: 9/15/2009 - 9/15/2009
Location: Holiday Inn Boston - Somerville
Somerville, MA
View Details

September Networking Breakfast
Dates: 9/15/2009 - 9/15/2009
Location: Holiday Inn Boston - Somerville
Somerville, MA
View Details

LINUXWORLD SUMMIT 2009
Dates: 9/1/2009 - 9/1/2009
Location: IDG World Expo
Framingham, MA
View Details