我有一個名爲「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)' />";
}
所以一切後,這是我的網頁看起來像:
現在,當用戶單擊「提交」按鈕(使用C#)時,如何從文本框中收集數據並將它們保存在csv/txt文件中?
我已經看到的一個明顯問題是,我將行添加到HTML表格的方式不允許我爲每個文本框設置ID
。有什麼辦法解決這個問題嗎?
我真的很感激任何幫助,這將允許我保存數據w/o更改addRow()
謝謝!
等待我的聲望得分超過10,所以我可以添加一張我在這裏問的照片:P – AlwaysANovice
我認爲你的意思是唯一的'name'屬性,對吧?他們必須是獨一無二的,否則你只是用每一行都覆蓋它們。你可以做'name = \'tb_state [「+ Math.Round(Math.Random()* 50)] +」\「',這會給每個'tb_state'值''tb_state'數組發送一個唯一的索引服務器,或者跟蹤您添加的當前行號(這將是更可取的)。在表格周圍包裹一個「表格」並提供一個「提交」按鈕,然後你所剩下的就是保存提交數據的服務器端代碼。 –
或者,您可以將AJAX添加到'addRow()'動態發送每一行,因爲它被添加到'表'到服務器。 –