I want to add dynamic literal control and dynamic button.
so I try like this:
Literal lit = new Literal();
lit.Text = lit.Text + theRow["CaseExampleDescription"].ToString() + "\n\n";
Button btn = new Button();
btn.ID = "btn1";
lit.Controls.Add(btn);
but there is error -System.Web.UI.WebControls.Literal’ does not allow child controls.
How can I solve this?
You need to use a container that would hold your
child controls (like button, literal, etc.),thus it needs to implement WebControl Class.Literal is not deriving from system.Web.UI.WebControls namespace. Thus can not have collection of controls.
Basically you need to use a server control as the base class that defines the methods, properties and events common to all controls in the System.Web.UI.WebControls namespace.
How to do your task ?
Read the following MSDN article – How to: Add Controls to an ASP.NET Web Page Programmatically