Solved

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

Posted on 2007-11-16
2
645 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now