views:

69

answers:

1

Hello, I have a drop down list that appears throughout my site. For code re-usability purposes I made it a Server Control. Everything was working great. However I would like to add a event handler for SelectedIndexChanged

This is not working for me:

this.OnSelectedIndexChanged = "CultureSelectorControl1_SelectedIndexChanged";

followed by:

 protected void CultureSelectorControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // code here
        }

Please let me know what to change here. Thanks!

A: 

Looks like you are confusing the markup syntax for event hookups with the code equivalent. If you are subclassing DropDownList, then just use this code for handling the event internally:

protected override void OnSelectedIndexChanged(EventArgs e)
{
    // do your stuff here

    // notify other subscribers
    base(e);
}

Controls shouldn't subscribe to their own events.

If you are using a user control instead which contains the DropDownList control and you want to handle the event in the user control, then you can either do it this way in code during Page_Init:

protected void Page_Init(object sender, EventArgs e)
{
    CultureSelectorControl1.SelectedIndexChanged += CultureSelectorControl1_SelectedIndexChanged;
}

Or on the markup for the DropDownList:

<asp:DropDownList ID="CultureSelectorControl1" Runat="server" OnSelectedIndexChanged="CultureSelectorControl1" />
Sam
Thanks Sam! This worked great!
aron