简体   繁体   中英

Getting Value for Dynamically Loaded User Control

I have a user control that is dynamically loaded by my page. The user control contains a number of multi-select listboxes and other controls.

Most of this works just fine. On postback, my list box controls are correctly populated (without me having to repopulate them); however, any selected items are not set. That is, GetSelectedIndices() returns an empty set.

What is the correct sequence here? How can my dynamically loaded user control use standard ASP.NET to get the selected list box values?

While Microsoft recommends that dynamic controls be recreated in the Page_PreInit event, in fact I found that all controls are null during the event. Therefore, I could not add the dynamic control to my Placeholder control because the Placeholder control is still null .

However, I found that if I created my dynamic control in the Page_Init event, that things seem to work fine.

Note that when you load a control dynamically, ASP.NET will generate any events for that control that would've normally fire before the dynamic control was created. So my user control can have all event handlers I want as I'd normally write them, and everything seems to work okay.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM