Server controls inside a composite control do not fire events within control
Posted on 2007-11-16
With much pain i have created a custom control which renders the control with a decent look and feel. I could possibly handle that. But the matter of concern is that the server controls (i have many listbox to name one) are not invoking any events. I will explain more about the custom control. It is quite a simple custom control that inherits from CompositeControl and INamingContainer. The control has an asp.net button and an asp.net listbox server control. In the CreateChildControls method of the CustomControl - ImagePicker is its name, i have associtated the selectedindexchanged event on the listbox. The behaviour at runtime is weird. The page which hosts the CustomControl posts back successfully, but the event is never invoked. Note that the event handler is also inside the control itself. I am not talking about exposing the events to the external world which i know will require me to implement the interfaces IPostBackDataHandler and IPostBackEventHandler. I have infact implemented those interfaces just in case i understood it wrong, but it wont help.
Following is the code that i added in CreateChildControls method:
listCategories = new ListBox();
listCategories.Height = Unit.Pixel(120);
listCategories.AutoPostBack = true;
listCategories.BackColor = Color.Gray;
listCategories.BorderStyle = BorderStyle.None;
listCategories.BorderWidth = 0;
listCategories.EnableViewState = true;
listCategories.SelectedIndexChanged += new EventHandler(listCategories_SelectedIndexChanged);
The method listCategories_SelectedIndexChanged is never fired. Neither the SelectedIndex of the listbox gets changed.