How to generate dynamic Divs on


I'm working with some designs in , I want to save the creation of x number of ASP pages, it is worth mentioning that I am working with

asked by matteo 09.05.2016 в 18:24

3 answers


If seeing such or such div depends on a dynamic event in the browser, it is ok to use javascript, but if not, it is recommended that you limit it directly on the server

 @if (PasaTalCosa) {

As you see in the example, you can also put each of the divs in its own partial, for doubts you need to use it in different actions.

answered by 09.05.2016 в 19:22

Creating elements from the server code of your .aspx page is very simple, in your .aspx.cs:

private void CrearItems()

 // Crear elemento div genérico
 HtmlGenericControl item = new HtmlGenericControl("div");

 // Creamos los elementos de la lista de forma dinámica
 foreach (var elemento in tuColeccion)
     // Establecer atributos necesarios
     // item.Attributes.Add("class", "item");
     // item.Id
     // item.InnerHtml
     // etc...

     // Añadir elemento div al elemento contenedor


In your .aspx you only need to define a container control to which the <div> will be added. It can be an web server control:

<asp:Panel ID= "contenedor"  runat = "server"></asp:Panel>

or, an html server control:

<div id="contenedor" class="contenedor" runat="server"></div>
answered by 15.12.2016 в 10:08

What you need here is to make a SPA (Single Page Application). Have you seen Angular ??

You can see how this example works: link to get an idea.

answered by 10.05.2016 в 19:33