In an ASP.NET application, I’d like to dynamically render several controls with their properties coming in from an XML document. Here’s the kicker: I want to be able to dynamically determine the type of control. So, in my XML document, I have something like this:
<control>
<id>myControl1</id>
<type>CheckBox</type>
<text>Text For This Control</text>
</control>
I can get everything to work fine, as far as properties go, so long as I manually instantiate the new control as a checkbox…but I can’t seem to figure out how to make it a checkbox, versus a textbox or whatever based on the XML information…
You will probably want to be able to control the output beyond the type of Control. My suggestion:
You could also add a ReflectionControlProvider as a fallback for non registered types and let this use Activator.CreateInstance instead of throwing an Exception when encountering an unknown provider type. This way you get maximum flexibility for both specific control of rendering and dynamic creation.