简体   繁体   中英

Adding ASPX components from Code behind using C#

I have a requirement which adds html/aspx components from code behind. The components can be either Check box or Radio button and options for them comes from the Database. So what is the best approach to do this. Is user control helps here?

You should generally determine and add such controls via the page OnInit overload or Page_Init event, and be sure you do it on all requests, including postbacks.

Define local variables to hold the objects you may create (it could be a List<> if you don't know how many will exist ahead of time), and instantiate them as any other object, set their properties, and then add them to the Controls collection of the container item they should be in; By default, they will be added to the end of the container, but you can Insert them instead of Add if you like.

Assuming you re-create them like this every time, and do it during Init , you can then access them - including viewstate, if applicable - from the Load event/overload.

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