views:

591

answers:

4

Outline

OK, I have Google'd this and already expecting a big fat NO!! But I thought I should ask since I know sometimes there can be the odd little gem of knowledge lurking around in peoples heads ^_^

I am working my way through some excercises in a book for study, and this particular exercise is User Controls. I have cobbled together a control and would like to set the DefaultEvent for it (having done this for previous controls) so when I double-click it, the default event created is whatever I specify it to be.

NOTE: This is a standard User Control (.ascx), NOT a custom rendered control.

Current Code

Here is the class & event definition:

[System.ComponentModel.DefaultEvent("OKClicked")]
public partial class AddressBox : System.Web.UI.UserControl
{

    public event EventHandler OKClicked;

Current Result

Now, when I double click the the control when it is on a ASPX page, the following is created:

    protected void AddressBox1_Load(object sender, EventArgs e)
    {

    }

Not quite what I was expecting! So, my question:

Is it possible to define a DefaultEvent for a UserControl? Is it a hack? If it's [not] supported, is there a reason?


Side Note: How do we put underscores in code? I cant seem to put and escape char in?

A: 

Rob,

I tried the above code you supplied and it worked prefectly for me.

I'm using VS 2008 and .NET 2.

Martin
A: 

Here is a possible answer, without testing (like martin did).

In reflector, you will see that the DefaultEventAttribute allows itself to be inherited. In reflector, you see that the UserControl class has it's default event set to the Load event.

So the possible reason is that even though you are decorating your user control with the default event of OKClick, VS might still be thinking that the default event is load, as it's being inherited from UserControl whose default event is Load.

Just a high level guess at what might be happening.

Darren Kopp
A: 

@Martin I have tried this in both 2005 and 2008 (although I default to 2005 for work purposes - Sorry I should have mentioned that) but I can still not get this to work as expected. When double-clicking in 2008, I still get the default Load event handler being created.

@Darren Kopp Thanks for the input. I am afraid you may be right here (following some more investigation on Google). I may spend some time trying to prove the hypothesis by creating a Custom Rendered Control (which inherits from WebControl) and see if that works.

I guess it kind of makes sense in the a subclass should have at least the functionality provided by its base.. But attributes such as this? Weiirdddd...

Update will follow (although prob wont get time to play until tomorrow! Out for my birthday drinkies soon!)

Rob Cooper
A: 

OK, I checked this out, Inheriting from WebControl rather than UserControl.. All worked fine.

Looks like Darren Kopp takes the crown for this one! Thanks for the input!

Rob Cooper