Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top