Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expose OnSelectedIndexChanged event in CompositeControl

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

like image 474
NikolaiDante Avatar asked Dec 08 '22 08:12

NikolaiDante


2 Answers

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.

like image 136
Brian Rudolph Avatar answered Dec 09 '22 20:12

Brian Rudolph


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.

like image 33
BFree Avatar answered Dec 09 '22 20:12

BFree