Brendan Tompkins [MVP]

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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


Posted 04-21-2004 11:32 AM by Brendan Tompkins
Filed under:

[Advertisement]

Comments

Don Starkey wrote re: Auto Event Wire-Up for Controls
on 06-04-2007 11:37 AM

Trying to do something very similar where I want to choose which events show up in the designer for the web.ui.usercontrol.

Not having any luck as I cannot get any of my events to show up in the property window.  Please let me know if you have any additional information regarding this topic.

kasun gayantha wrote re: Auto Event Wire-Up for Controls
on 12-08-2008 11:19 AM

i'am very much interested in programming C#.NET

Add a Comment

(required)  
(optional)
(required)  
Remember Me?