如何在 asp.net 中从html表中将数据插入数据库
本文关键字:数据 插入 数据库 html asp net 中从 | 更新日期: 2023-09-27 17:55:24
如何将数据从 HTML 表添加到数据库中?
我的桌子是这样的:
html += "<table>";
html += "<tr><th>" + "A" + "</th><th>" + "B" + "</th><th>" + "C" + </th></tr>";
html += "<tr><td>" + "0" + "</td><td>" + "1" + "</td><td>" + "2" + </td></tr>";
html += "</table>";
我从服务器端调用 HTML。
如果你喜欢纯HTML,你可以使用像Knockout这样的JavaScript框架.js以及它。Knockout.js允许您使用JavaScript视图模型将数据注入到HTML中。按钮单击可以分配给视图模型中的 JavaScript 函数。JavaScript 函数可以执行 AJAX post 以调用服务器端的控制器操作方法 - 这会将数据插入数据库。
有关挖空的更多信息,请查看 http://knockoutjs.com
使用 System.Text.RegularExpressions (Regex) 搜索模式以替换表标记:
将<tr><th>
和<tr><td>
替换为空白,
将</th></tr>
和</td></tr>
替换为^^~~~~~~~~~~^^
表示结束。
将</td><td>
替换为||^^^^^^^^^||
表示分律计
string html = // your html table goes here
string[] lines = html.Split(new string[] { "^^~~~~~~~~~~^^" }, StringSplitOptions.None);
// now your html table is divided into lines, which means rows
// lines[0] = // the header
// lines[1] = // row 1
// lines[2] = // row 2
// lines[3] = // row 3
// ...
// ...
// line 1 is the header/column name
string[] columns = lines[0].Split(new string[] { "||^^^^^^^^^||" }, StringSplitOptions.None);
// columns[0] = // 1st column name
// columns[1] = // 2nd column name
// columns[2] = // 3rd column name
// ...
// ...
for (int i = 1; i < lines.Length; i++)
{
string[] data = lines[i].Split(new string[] { "||^^^^^^^^^||" }, StringSplitOptions.None);
// data[0] = // 1st data
// data[1] = // 2nd data
// data[2] = // 3rd data
// ...
// ...
}