gpt4 book ai didi

c# - 如何使用 C# 将数据从动态添加的行保存到 (csv/txt) 文件到 HTML 表?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:05:53 25 4
gpt4 key购买 nike

我有一个名为“newDataTable”的表,我在以下 JavaScript 的帮助下动态添加新行:

    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。有什么办法解决这个问题吗?

我将非常感谢任何帮助我在不更改 addRow() 的情况下保存数据的帮助,谢谢!!

最佳答案

也许你可以尝试这样的事情:

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

在这里,我只是通过添加 [] 更改了您的 input 的名称属性,它会将您的所有数据发布到一个数组中。因此,在服务器端,您只需遍历数组即可获取所有数据。

关于c# - 如何使用 C# 将数据从动态添加的行保存到 (csv/txt) 文件到 HTML 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8484148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com