When creating my html element in an automated way with c # is it replaced and not added?

0

What I want to do is create several rows with their respective element within an accordion, then I leave the code that I put in my event clicserver

public string RowsAdd = "";
            RowsAdd += "<section class='col-lg-12 text-center top-buffer'><section class='col-lg-2'> <label>Titulo</label></section><section class='col-lg-2'><label>Descripcion</label></section><section class='col-lg-2'><label>Horas</label></section><section class='col-lg-2'><label>Seccion</label></section><section class='col-lg-1'><label>Hecho</label></section></section><section class='col-lg-12 text-center top-buffer'><section class='col-lg-2'><input type='text' class='form-control mandatory'/> </section><section class='col-lg-2'><textarea rows='5' class='form-control mandatory'></textarea></section> <section class='col-lg-2' > <input type='number' class='form-control mandatory'/> </section> <section class='col-lg-2' > <input type='text' class='form-control mandatory'/> </section> <section class='col-lg-1' > <input type='checkbox' class='form-control mandatory'/> </section> <section class='col-lg-2' > <button class='btn btn-primary' style='margin-top: 0px; ' runat='server' ID='addRow' OnServerClick='addRow_OnServerClick'> Agregar </button> </section> <section class='col-lg-1' > <button class='btn btn-primary' style='margin-top: 0px; '> Remover </button> </section> </section><span>&nbsp;</span>";
            contantRows.InnerHtml = RowsAdd;

The problem is that if you do it but instead of putting the result of that query under my other row, it replaces it and every time I give it to add another new row it always stays in one because it does not put it under me it replaces the current one.

Html code:

<div style="height: auto;" runat="server" ID="contantRows">

    <section class="col-lg-12 text-center top-buffer" id="seccionador">

        <section class="col-lg-2">
            <label> Titulo </label>
        </section>

        <section class="col-lg-2">
            <label> Descripcion </label>
        </section>

        <section class="col-lg-2">
            <label> Horas </label>
        </section>

        <section class="col-lg-2">
            <label> Seccion </label>
        </section>

        <section class="col-lg-1">
            <label> Hecho </label>
        </section>

    </section>

    <section class="col-lg-12 text-center top-buffer">
        <section class="col-lg-2">
            <input type="text" class="form-control mandatory" />
        </section>

        <section class="col-lg-2">
            <textarea rows="5" class="form-control mandatory"></textarea>
        </section>

        <section class="col-lg-2">
            <input type="number" class="form-control mandatory" />
        </section>

        <section class="col-lg-2">
            <input type="text" class="form-control mandatory" />
        </section>
        <section class="col-lg-1">
            <input type="checkbox" class="form-control mandatory" />
        </section>

        <section class="col-lg-2">
            <button class="btn btn-primary" style="margin-top: 0px;" runat="server" ID="addRow"
            OnServerClick="addRow_OnServerClick"> Agregar </button>
        </section>
        <section class="col-lg-1">
            <button class="btn btn-primary" style="margin-top: 0px;"> Remover </button>
        </section>
    </section>
    <span>&nbsp; </span>
</div>
    
asked by David 29.06.2018 в 19:06
source

2 answers

1

Try this:

System.Web.UI.HtmlControls.HtmlGenericControl RowsAdd = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");

RowsAdd.Attributes.Add("class","unaNuevaFila");

RowsAdd.InnerHtml = "<section class='col-lg-12 text-center top-buffer'><section class='col-lg-2'> <label>Titulo</label></section><section class='col-lg-2'><label>Descripcion</label></section><section class='col-lg-2'><label>Horas</label></section><section class='col-lg-2'><label>Seccion</label></section><section class='col-lg-1'><label>Hecho</label></section></section><section class='col-lg-12 text-center top-buffer'><section class='col-lg-2'><input type='text' class='form-control mandatory'/> </section><section class='col-lg-2'><textarea rows='5' class='form-control mandatory'></textarea></section> <section class='col-lg-2' > <input type='number' class='form-control mandatory'/> </section> <section class='col-lg-2' > <input type='text' class='form-control mandatory'/> </section> <section class='col-lg-1' > <input type='checkbox' class='form-control mandatory'/> </section> <section class='col-lg-2' > <button class='btn btn-primary' style='margin-top: 0px; ' runat='server' ID='addRow' OnServerClick='addRow_OnServerClick'> Agregar </button> </section> <section class='col-lg-1' > <button class='btn btn-primary' style='margin-top: 0px; '> Remover </button> </section> </section><span>&nbsp;</span>";

contantRows.Controls.Add(RowsAdd);

you create a new div, you put a class, you define the innerhtml, and finally you add it to contantRows

    
answered by 29.06.2018 / 20:14
source
1

this is the problem:

public string RowsAdd="";

You are redefining your local variable every time you enter the cycle or event or class or wherever you are putting it.

Do your global variable to the method or cycle so do not erase it

 public string RowsAdd ="";
 for(int i=0;i<x;i++)
 {
      RowsAdd += "<section class='col-lg-12 text-center top-buffer'><section class='col-lg-2'> <label>Titulo</label></section><section class='col-lg-2'><label>Descripcion</label></section><section class='col-lg-2'><label>Horas</label></section><section class='col-lg-2'><label>Seccion</label></section><section class='col-lg-1'><label>Hecho</label></section></section><section class='col-lg-12 text-center top-buffer'><section class='col-lg-2'><input type='text' class='form-control mandatory'/> </section><section class='col-lg-2'><textarea rows='5' class='form-control mandatory'></textarea></section> <section class='col-lg-2' > <input type='number' class='form-control mandatory'/> </section> <section class='col-lg-2' > <input type='text' class='form-control mandatory'/> </section> <section class='col-lg-1' > <input type='checkbox' class='form-control mandatory'/> </section> <section class='col-lg-2' > <button class='btn btn-primary' style='margin-top: 0px; ' runat='server' ID='addRow' OnServerClick='addRow_OnServerClick'> Agregar </button> </section> <section class='col-lg-1' > <button class='btn btn-primary' style='margin-top: 0px; '> Remover </button> </section> </section><span>&nbsp;</span>";
        contantRows.InnerHtml = RowsAdd;
 }

The other option would be to make your innerHTML + = so you do not erase the previous result

contantRows.InnerHtml += RowsAdd;
    
answered by 29.06.2018 в 19:25