View Full Version : Dynamic user controls

01-04-2011, 10:50 AM

I'm evaluating user controls (.ascx) to use them in a web I'm developing, and I have found that work different if they are static (placed in the .aspx at design time) or dynamic (loaded in a PlaceHolder or Panel in .aspx Page_Load()).

For example: inside the .ascx control I use a DropdownList (with AutoPostback), and I want update other data displayed in the control depending on the selected value in the dropdownlist. If the .ascx is static, after a postback, the selected value is available in the Page_Load() event of the control. However, if the .ascx is dynamic, the selected value is available in the Page_PreRender() event (in Page_Load() is available the previous value).

Is correct this behaviour? Is it possible to make the dynamic user controls work like static in terms of page events?
I don't know if I'm doing something wrong (or forgetting something).


01-04-2011, 10:06 PM
I'm pretty sure in the past I've done what you are doing, using dropdownlist controls to trigger other dropdownlist controls, both as user controls. But I do remember had to use some workarounds as it wasn't as straightforward as one would think. Cascading dropdownlist are tricky in general.

That was awhile ago, and if I were to do that now, I would use Ajax as it would be easier. You could code it yourself (make your own control using javascript/jquery) or even easier, use ASP.NET Ajax Control Toolkit. Here's the cascading dropdown control:

I haven't tried it as a usercontrol, but I would imagine it would be easier since it's all one control, rather than having to chain several controls, like you are doing.

There are also other controls, so you may find the one you want for your purpose as maybe you aren't cascading your dropdowns, but want to trigger something us, like a listbox, etc. Look at the sample list on that page of the right menu.

Note I don't know which ASP.NET version you are using (2.0, 3.5, 4.0) so you'll have to download the appropriate Ajax Control Toolkit for your version, like version 1.x for ASP.NET 2.0, version 3.x for ASP.NET 3.5 (and yes they skipped version 2.x as the version numbering system confused people). I forget, but ASP.NET 4.0 might have it included by default (I don't have VS 2010 open right now so I can't check).

01-06-2011, 09:45 AM
Thanks for your response, but the problem is the load of dynamic user controls, not the update of cascading controls. I've solved my problem loading the user control in Page_Init().