I am creating a server control that will sit in a aspx page. When the user selects a menu option, html controls (selects, inputs, etc) will have be added dynamically. I can do it using a user control, but I’m not sure how to go about it in a server control.
Can anyone tell me how I can add dynamic html into the control after it’s already sitting in a page?
Since you are planning to add several controls as the output of your server control, you should consider inheriting from CompositeControl, this control is designed to work with several constituent controls, minimizing the code needed to do common things like keeping state and handling constituent control events
You need to override the CreateChildControls to add child controls. At the end of this method you should use
this.ChildControlsCreated = true;to specify if the child controls were created, this is necessary because theCreateChildControlsis called several times during the ASP.Net page life-cycleYou need to apply the same rules that you would typically apply to any server control, for example implement the INamingContainer interface to ensure that child controls will have a unique client ID
This is a simple example: