Using Page.ParseControl to add new control from Control Tag
Hi,
Sometimes we might have a requirement where by we need to parse the tags and create some controls with that in asp.net. Think of a situation where you do not what type of control will be create. You need to pass the control from an xml file/ or database. There can be many properties to the control, which will be passed. Passing these properties individually can be difficult, because you do not the kind of control itself.
In this kind of situation if we can pass the full formatted control tag as a string which will be parsed and updated then it can be great as we can easily apply all the property we want to apply.
This can be achieved with the help of the ParseControl method of the Page class. Below is an example of what can be done.
string strCtrlhtml = "<asp:TextBox id=\"name\" Text=\”SomeDefaultValue\” runat=\"server\" />"
Control ctrlDynamicallyCreated = Page.ParseControl(strCtrlhtml);
anyPlaceHolder.Controls.Add(ctrlDynamicallyCreated);
As you swa, whe can add any kind of server control with the help of the ParseControl method.
This can be very useful when you want to add some dynamic control at the runtime or even keep changing the dynamic control.
Vikram
P.S. I do not mean that this is the only way to do it, but this can also be helpful for certain condition.