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
– Marco Souza