.NET Tip: Creating and Using Your Own Events Wisconsin

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

Acumium
608 310 9700 x 522
5133 West Terrace Drive Suite 300
Madison, , WI
Wireless Direct
1.866.707.8498
BOX 71101
shorewood, WI
HarrisData
800-225-0585
13555 Bishops Court
Brookfield, WI
IFS
414-577-5191
12000 W. Park Place
Milwaukee, WI
Xorbix Technologies Inc.
414-277-5044
759 N. Milwaukee St.
Milwaukee, WI
Visionary Computer Solutions
262-365-9430
PO Box 406
Grafton, WI
R S InfoCon, Inc.
262-898-7456
2320 Renaissance Blvd
Sturtevant, WI
HarrisData
262-784-9099
13555 Bishop's Court, Suite 300
Brookfield, WI
Electroniclaim
262-240-9700
11357 N. Port Washington Rd
Mequon, WI
R.E. Coker and Associates, Inc.
262-723-8104
108 W Court St.
Elkhorn, WI

.NET Tip: Creating and Using Your Own Events

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

Wireless Direct

1.866.707.8498
BOX 71101
shorewood, WI
http://www.prepaidwireless.2ya.com

Regional Articles
- .NET Tip: Creating and Using Your Own Events Appleton WI
- .NET Tip: Creating and Using Your Own Events Baraboo WI
- .NET Tip: Creating and Using Your Own Events Beaver Dam WI
- .NET Tip: Creating and Using Your Own Events Beloit WI
- .NET Tip: Creating and Using Your Own Events Brookfield WI
- .NET Tip: Creating and Using Your Own Events Burlington WI
- .NET Tip: Creating and Using Your Own Events Cedarburg WI
- .NET Tip: Creating and Using Your Own Events Chippewa Falls WI
- .NET Tip: Creating and Using Your Own Events Cudahy WI
- .NET Tip: Creating and Using Your Own Events De Pere WI
- .NET Tip: Creating and Using Your Own Events Delavan WI
- .NET Tip: Creating and Using Your Own Events Eau Claire WI
- .NET Tip: Creating and Using Your Own Events Elkhorn WI
- .NET Tip: Creating and Using Your Own Events Fond Du Lac WI
- .NET Tip: Creating and Using Your Own Events Fort Atkinson WI
- .NET Tip: Creating and Using Your Own Events Franklin WI
- .NET Tip: Creating and Using Your Own Events Green Bay WI
- .NET Tip: Creating and Using Your Own Events Hartland WI
- .NET Tip: Creating and Using Your Own Events Janesville WI
- .NET Tip: Creating and Using Your Own Events Kaukauna WI
- .NET Tip: Creating and Using Your Own Events Kenosha WI
- .NET Tip: Creating and Using Your Own Events La Crosse WI
- .NET Tip: Creating and Using Your Own Events Lake Geneva WI
- .NET Tip: Creating and Using Your Own Events Manitowoc WI
- .NET Tip: Creating and Using Your Own Events Marinette WI
- .NET Tip: Creating and Using Your Own Events Marshfield WI
- .NET Tip: Creating and Using Your Own Events Menasha WI
- .NET Tip: Creating and Using Your Own Events Menomonee Falls WI
- .NET Tip: Creating and Using Your Own Events Menomonie WI
- .NET Tip: Creating and Using Your Own Events Merrill WI
- .NET Tip: Creating and Using Your Own Events Middleton WI
- .NET Tip: Creating and Using Your Own Events Milwaukee WI
- .NET Tip: Creating and Using Your Own Events Mosinee WI
- .NET Tip: Creating and Using Your Own Events Mukwonago WI
- .NET Tip: Creating and Using Your Own Events Muskego WI
- .NET Tip: Creating and Using Your Own Events Neenah WI
- .NET Tip: Creating and Using Your Own Events New Berlin WI
- .NET Tip: Creating and Using Your Own Events Oak Creek WI
- .NET Tip: Creating and Using Your Own Events Oconomowoc WI
- .NET Tip: Creating and Using Your Own Events Onalaska WI
- .NET Tip: Creating and Using Your Own Events Oshkosh WI
- .NET Tip: Creating and Using Your Own Events Pewaukee WI
- .NET Tip: Creating and Using Your Own Events Racine WI
- .NET Tip: Creating and Using Your Own Events Rhinelander WI
- .NET Tip: Creating and Using Your Own Events Rice Lake WI
- .NET Tip: Creating and Using Your Own Events River Falls WI
- .NET Tip: Creating and Using Your Own Events Schofield WI
- .NET Tip: Creating and Using Your Own Events Shawano WI
- .NET Tip: Creating and Using Your Own Events Sheboygan WI
- .NET Tip: Creating and Using Your Own Events South Milwaukee WI
- .NET Tip: Creating and Using Your Own Events Stevens Point WI
- .NET Tip: Creating and Using Your Own Events Sturgeon Bay WI
- .NET Tip: Creating and Using Your Own Events Sun Prairie WI
- .NET Tip: Creating and Using Your Own Events Superior WI
- .NET Tip: Creating and Using Your Own Events Thiensville WI
- .NET Tip: Creating and Using Your Own Events Two Rivers WI
- .NET Tip: Creating and Using Your Own Events Watertown WI
- .NET Tip: Creating and Using Your Own Events Waukesha WI
- .NET Tip: Creating and Using Your Own Events Waupaca WI
- .NET Tip: Creating and Using Your Own Events Wausau WI
- .NET Tip: Creating and Using Your Own Events West Bend WI
- .NET Tip: Creating and Using Your Own Events Whitewater WI
- .NET Tip: Creating and Using Your Own Events Wisconsin Rapids WI
Related Local Events
2008 Early Stage Symposium
Dates: 11/5/2008 - 11/6/2008
Location: Monona Terrace
Madison WI
View Details

Wisconsin Entrepreneurs' Conference
Dates: 6/9/2008 - 6/10/2008
Location: Hyatt Regency Hotel
Milwaukee WI
View Details
Rate Article
     
Articles Insider

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Advertising Engineering Home Services Retail & Consumer Services
Business Services Entertainment Industrial Goods & Services Software
Career Family Insurance Technology
Cars Financial Services Internet Telecommunications
Computer Hardware Food & Beverage Legal Transportation & Logistics
Construction Health Pets Travel
Education Home Electronics Real Estate Wedding