Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to add same div dynamically asp.net code behind ...I want to add it dynamically..
Posted

1 solution

It depends on what you want to do with it, but here's probably the easiest way.

On your ASP page, you would have a parent container that you wish to add your DIV to, such as:

ASP.NET
<div id="divContainer" runat="server">
      <!-- added DIV will go inside here -->
</div>


Make sure you have the 'runat="server"' element tag added to this element, otherwise your code behind will not be able to see it.

And then in your code behind:

C#
divContainer.InnerHtml = "<div style=\"color:#ff0000;\">some new content goes here</div>";


Or for more control over the element once created, use:
C#
System.Web.UI.HtmlControls.HtmlGenericControl divContent = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
divContent.ID = "divContent";
divContent.Style.Add(HtmlTextWriterStyle.Color, "Red");
divContent.InnerHtml = "some new content goes here";
divContainer.Controls.Add(divContent);
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900