Home > DeveloperSection > Forums > How to Add a div contains asp Label from code behind in asp.net
Ankita Pandey
Ankita Pandey

Total Post:183

Posted on    January-07-2015 10:20 PM

 .NET C#  ASP.Net 

 1 Reply(s)
 1193  View(s)
Rate this:

I tried to add a div which contains an asp:Label on a button click event.
The following code which I used. CODE

string htmlString = "<div><asp:Label ID=\"l1\" runat=\"server\" Text=\"WELCOME\"></asp:Label></div>";

Panel1.Controls.Add(new LiteralControl(htmlString));

But I am not able to view that particular div.
Please try to solve me this.

Thanks in advance.

Mark Devid
Mark Devid

Total Post:102

Posted on    January-08-2015 2:48 AM

Adding the markup for a server control like that will not process the server control but simply will put that markup in the produced HTML

You can use a HTmlGenericControl to do that

using System.Web.UI.HtmlControls;


Label lbl = new Label();

lbl.Text = "WELCOME";


HtmlGenericControl div = new HtmlGenericControl("div");




To add a DataList inside the div you can do the following

DataList dl = new DataList();

//Init your DataList here

dl.DataSource = dt; // `dt` is a DataTable with my data



HtmlGenericControl div = new HtmlGenericControl("div");





Don't want to miss updates? Please click the below button!

Follow MindStick