Solved

Server controls inside a composite control do not fire events within control

Posted on 2007-11-16
2
651 Views
Last Modified: 2012-08-13
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;
            this.Controls.Add(listCategories);
            listCategories.SelectedIndexChanged += new EventHandler(listCategories_SelectedIndexChanged);

The method listCategories_SelectedIndexChanged is never fired. Neither the SelectedIndex of the listbox gets changed.
0
Comment
Question by:sudhijss
2 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 20300468
Just go ahead and post all the code of your composite control, and all the code of the aspx page you are using this control in.  It is possible to do what you want without anything too fancy.
0
 
LVL 1

Accepted Solution

by:
sudhijss earned 0 total points
ID: 20301016
Got it! Must have been a stupid mistake i guess. I was adding items to the list inside the RenderContents method. I moved that databinding code to the CreateChildControls and lo the event fires. I must ve not understood the page lifecycle appropriately. Thanks for offering help anyway.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question