开发者

Adding ASPX components from Code behind using C#

开发者 https://www.devze.com 2023-03-05 04:38 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消