2011-12-13 40 views
4

我有一個名爲「newDataTable」裏我加入新行動態與下面的JavaScript的幫助下表:如何使用C#將數據從動態添加行保存到(csv/txt)文件到HTML表格中?

function addRow() 
    { 
     //add a row to the rows collection and get a reference to the newly added row 
     var table = document.getElementById('newDataTable'); 
     var lastRow = table.rows.length; 
     var newRow = table.insertRow(lastRow); 

     //add 6 cells (<td>) to the new row and set the innerHTML to contain text boxes 

     var newCell = newRow.insertCell(0); 
     newCell.innerHTML = "<label> City: </label>"; 

     newCell = newRow.insertCell(1); 
     newCell.innerHTML = "<input type='text' name='tb_city'/>"; 

     newCell = newRow.insertCell(2); 
     newCell.innerHTML = "<label> State: </label>"; 

     newCell = newRow.insertCell(3); 
     newCell.innerHTML = "<input type='text' name='tb_state'/>"; 

     newCell = newRow.insertCell(4); 
     newCell.innerHTML = "<input title='Add Row' name='addButton' type='button' value='Add' onclick='addRow()' />"; 

     newCell = newRow.insertCell(5); 
     newCell.innerHTML = "<input title='Remove Row' name='deleteButton' type='button' value='Delete' onclick='removeRow(this)' />"; 
    } 

所以一切後,這是我的網頁看起來像: Screen Snippet of Form

現在,當用戶單擊「提交」按鈕(使用C#)時,如何從文本框中收集數據並將它們保存在csv/txt文件中?
我已經看到的一個明顯問題是,我將行添加到HTML表格的方式不允許我爲每個文本框設置ID。有什麼辦法解決這個問題嗎?

我真的很感激任何幫助,這將允許我保存數據w/o更改addRow()謝謝!

+0

等待我的聲望得分超過10,所以我可以添加一張我在這裏問的照片:P – AlwaysANovice

+0

我認爲你的意思是唯一的'name'屬性,對吧?他們必須是獨一無二的,否則你只是用每一行都覆蓋它們。你可以做'name = \'tb_state [「+ Math.Round(Math.Random()* 50)] +」\「',這會給每個'tb_state'值''tb_state'數組發送一個唯一的索引服務器,或者跟蹤您添加的當前行號(這將是更可取的)。在表格周圍包裹一個「表格」並提供一個「提交」按鈕,然後你所剩下的就是保存提交數據的服務器端代碼。 –

+0

或者,您可以將AJAX添加到'addRow()'動態發送每一行,因爲它被添加到'表'到服務器。 –

回答

3

也許你可以嘗試這樣的:

newCell.innerHTML = "<input type='text' name='tb_city[]'/>"; 
newCell.innerHTML = "<input type='text' name='tb_state[]'/>"; 

在這裏,我只是通過增加[]改變了你的input S的name屬性,它會發佈陣列中的所有數據你。所以在服務器端,您只需遍歷數組來獲取所有數據。

+0

哦!那很有意思!你能否給我提供一個類似例子的鏈接?請?! TY! – AlwaysANovice

+0

我很長一段時間沒有做過任何ASP,但這可能是相互衝突的:http://stackoverflow.com/questions/4561686/handling-arrays-of-html-input-elements-with-request-form-like- PHP – TrexXx

相關問題