This post marks my inaugural attempt at resolving a persistent issue that has been plaguing me for several days now. The crux of the matter lies in the following block of HTML code:
<fieldset>
<legend>Certifictions</legend>
<div runat="server" id="divCert">
<input runat="server" type="button" id="btnAddCert" class="Botones" value="Agregar Certificación" onclick="AddCertification()" />
<input runat="server" type="button" id="btnRemoveCert" class="Botones" value="Quitar Certificación" onclick="RemoveCert()" />
<br/>
</div>
</fieldset>
The crux of the challenge revolves around dynamically adding and removing text fields to divCert
through JavaScript. Here's the pertinent code snippet:
var count1=0;
function AddCertification() {
document.getElementById("divCert").innerHTML +=
"<input type='Text' runat='server' name='txCert_" +
(count + 1) + "' class='textBox' id='txCert" + (count1 + 1) +
"' placeholder='Insert Text'/>";
count1 = count1 + 1;
}
function RemoveCert() {
var parent;
var eleRemove
if (count1 > 0) {
parent = document.getElementById("divCert");
eleRemove = document.getElementById("txCert" + (count1).toString());
parent.removeChild(eleRemove);
count1 = count1 - 1;
}else {
window.alert("There's nothing to remove");
}
}
My ultimate objective is to retrieve the value of each textbox generated within the codebehind file, store this information, and subsequently write it into a separate file. A prospective solution might look something akin to the following:
List<Object> list = new List<object>();
foreach(TextBox found in divCer.Controls){
list.Add(found.Value);
}
I am acutely aware that the aforementioned code snippet will not function as intended, however, the essence of this endeavor is to extract values from each dynamically-generated textbox.