Dynamic button inside dynamic literal control

I want to add a dynamic dynamic control and a 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 an error -System.Web.UI.WebControls.Literal 'does not allow child controls.

How can i solve this?

+4
source share
2 answers

You need to use a container that will contain your child controls (like button, literal, etc.), , so it needs to implement the WebControl class.

A literal is not inferred from the system.Web.UI.WebControls namespace. Therefore, there cannot be a collection of controls.

Basically, you need to use the server control as a base class that defines methods, properties, and events that are common to all controls in the System.Web.UI.WebControls namespace.

How to complete your task?

Read the following MSDN article - How to Add Controls to ASP.NET Software Web Page

+3
source

As this article indicates, literal control is nothing but a way of displaying text on a page. Therefore, it does not support child controls. The reason it has a collection of controls is because it inherits from system.web.ui.control.

What are you going to use to support child controls such as asp.net panel .

0
source

All Articles