2016-09-13 22 views
0

這是我的第一篇文章。現在我已經有幾天遇到麻煩了。我有這樣的HTML代碼:獲取Javascript生成的文本輸入值,並使用.Net存儲它#

<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> 

正如你所看到的,我通過JavaScript添加和刪除文本字段divCert。這是代碼:

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"); 
    } 
} 

現在,我想要做的就是在aspx.cs文件中的代碼隱藏生成的文本框的值,儲存,並將其寫入到文件中。這樣的事情:

List<Object> list = new List<object>(); 
    foreach(TextBox found in divCer.Controls){ 
    list.Add(found.Value); 
} 

我知道代碼不會工作,但類似的東西是我想要做的每個文本框生成的值。

回答

0

除非你有一個理由,這些控件的服務器端的情況下,你可以通過

Request.Form["txCert" + i]; 

其中count可以「傳遞」到服務器端,i得到他們的價值觀可以遍歷count

+0

謝謝你的幫助。這很有幫助。並解決了我的問題。 – Richard

+0

[when-someone-answers](http://stackoverflow.com/help/someone-answers) –

相關問題