How to Recover C# Dynamic Fields for SQL

Asked

Viewed 92 times

0

I’m making an ASP site that has dynamic fields in js:

a = 0;
function GetDynamicTextBox(value) {
    a++;
    return '<div class="container bg-light p-3 mb-2"><div class="form-group row justify-content-center">'+
        '<div class="col-sm-1"> <label ID = "lblPag_'+a+'" class="col-form-label" >Páginas:</label ></div>' +
        '<div class="col-sm-1">' +
            '<input name = "txtPag' + a + '" type="text" value = "' + value + '"  class="form-control"/> </div>' +
        '<div class="col-sm-1">' +
            '<label ID="lblPublique' + a + '" class="col-form-label">até</label></div>' +
        '<div class="col-sm-1">' +
            '<input name = "txtPagF' + a + '" type="text" value = "' + value + '"  class="form-control"/> </div>' +
        '<div class="col-sm-1">' +
            '<label ID="lblPublique'+a+'" class="col-form-label">Descrição:</label></div>' +
        '<div class="col-sm-2">' +
            '<input name = "txtDesc' + a + '" type="text" value = "' + value + '"class="form-control"/> </div></div>' +
        '<div class="form-group row justify-content-center">' +
            '<div class="col-sm-1">' +
                '<label ID="lblPublique' + a + '" class="col-form-label">Autor:</label></div>' +
            '<div class="col-sm-2">' +
                '<input name = "txtAutor' + a + '" type="text" value = "' + value + '"  class="form-control"/> </div>' +
            '<div class="col-sm-4">' +
                '<input type="file" name="fileUp'+a+' value="'+value+'"></div>' +
            '<div class="col-sm-1">' +
                '<input type="button" value="Remover" class="btn btn-danger" onclick = "RemoveTextBox(this)" /></div></div></div>'
}
function AddTextBox() {
    var div = document.createElement('DIV');
    div.innerHTML = GetDynamicTextBox("");
    document.getElementById("TextBoxContainer").appendChild(div);
}

function RemoveTextBox() {
    var ele = document.getElementById("TextBoxContainer");
    if (ele.lastChild) {
        ele.removeChild(ele.lastChild);
    }
}

function RecreateDynamicTextboxes() {
    var values = eval('<%=Values%>');
    if (values != null) {
        var html = "";
        for (var i = 0; i < values.length; i++) {
            html += "<div>" + GetDynamicTextBox(values[i]) + "</div>";
        }
        document.getElementById("TextBoxContainer").innerHTML = html;
    }
}
window.onload = RecreateDynamicTextboxes;

And save the fields in SQL, but I would need to save all the fields that can be added, ie several txtN.Text. How could it be done? I’m confused if I use a list or some other command. CS:

protected void btnSalvar1_Click(object sender, EventArgs e)
    {
        var cnx = new Conexao();
        var con = cnx.ConexaoBD();
        var XX = @"DECLARE @ID INT INSERT INTO POLIS_RI_PUBLICACAO(NOME, CATEGORIA) VALUES('"+txtDesc.Text+"', '"+dpdCategoria.Text+"') SET @ID = (SELECT SCOPE_IDENTITY()) INSERT INTO POLIS_RI_ARTIGO(IDPUB, PGINI, PGFIN, NOMEAUTOR) VALUES(@ID, '"+txtPag.Text+"', '"+txtPag1.Text+"', '"+txtAutor.Text+"')";
        var cmd = new SqlCommand(XX, con);
        var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    }
  • You need a loop for(i = 0; i <=A;i++)ː var he = Document.getElementById("txtDesc.Text" + i);} .... where your A is equal to your variable (a = 0;)... so you would have the field list

1 answer

1


Maisa, from what I can understand from your code, you’re wanting to store the fields that are created dynamically through columns. But I suggest you administer these fields vertically, using relationships. In this horizontal template creating columns for each field, one hour your table will be unsustainable.

Example: Create a table that will store the fields:

ID | Fieldname

  • 1 | Pagini
  • 2 | Paginfo
  • 3 | Author

Then create a table that will store the value of each field with a foreign key pointing to the previous table.

Campo_id | Value

  • 1 | Home.html
  • 2 | FAQ.html
  • 3 | Paulo Coelho
  • 3 | Clarisse Lispector

Then to know which fields have been added and the values assigned to that field you only need to make a Join between tables. If you still want the field name to be in columns and the values in rows, you can apply a table pivot to sql when performing your query.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.