CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

Auto Event Wire-Up for Controls

Ever noticed that when you double-click on an ASP.NET UserControl, the code-behind loads, with the cursor in the Page_Load event?  Well, this as far as I can tell, is because the UserControl class contains a DefaultEvent attribute, specifying “OnLoad“ as the event.  Thus, the VS IDE automatically wires up this event and writes the skeleton code below for you:

private void Page_Load(object sender, System.EventArgs e)
{
}

Problem is, I don't want my UserControls to tell my IDE to do this for me. This is because I generally don't think it makes good design sense to use the Page_Load event  from a UserControl.  If you've developed ASP.NET apps with multiple controls on a page, you know what I mean.  Your app logic may be hiding/showing controls ala Dave Burke's post here.  Wheneach UserControl is doing something on Page_Load, things get done too often, and often unnecessarily.  Actually, I often delete this method all togtehter. What makes more sense is to have an OnVisible event or  what I've done is added a Show() method stub to my UserControlBase class that is intended to handle the initial setup stuff. 

So when I double click on the designer panel, I don't want the Page_Load method to be added and wired for me. This is annoying when I've deleted it.  One thing I can do is create an Attribute for my base class specifying and “OnVisible” event if I had one or any other event for that matter.  But what if I don't want any event wired up?  Does anyone know how to keep the IDE from autmatically wiring up events for a class? 

-Brendan



Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!

Our Sponsors