views:

608

answers:

3

If I have a DropDownList control that makes up part of a CompositeControl how can I expose the SelectedIndexChanged event to the consuming aspx page?

Thanks

+3  A: 

Here's what you do. First declare an event like this:

public event EventHandler SelectedIndexChanged;

Then, internally, hook up to the DropDownList's SelectedIndexChangedEvent. In your event handler do something like this:

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.SelectedIndexChanged != null)
            {
                this.SelectedIndexChanged(sender, e);
            }
        }

All you're really doing is wrapping the original event and re-raising it.

EDIT: See Brian Rudolph's answer. That's in fact a much simple way of doing it.

BFree
Ok, makes sense. I was getting into a mess with delegates. That's exactly what I was looking for. I'll give it a shot in the morning - thanks!
DeletedAccount
+5  A: 

There is a much simpler way that is a direct pass through.

Try this:

    public event EventHandler SelectedIndexChanged
    {
        add { this.TargetControl.SelectedIndexChanged += value; }
        remove { this.TargetControl.SelectedIndexChanged -= value; }
    }

[Edit] Unless of course you need to inject custom logic.

Brian Rudolph
Worked like a charm. Thank you very much!
DeletedAccount
A: 

Thankssssssss