How to insert data into database from html table in asp.net
How do I add data to a database from an HTML table?
My table is like this:
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>";
I am calling HTML from the server side.
source to share
if you like plain HTML you can use a JavaScript framework like Knockout.js along with it. Knockout.js allows you to inject data into HTML using JavaScript view models. Clicking on the button can assign JavaScript functions in the view model. A JavaScript function can perform an AJAX entry to invoke a server-side controller action method that will insert data into the database.
For more information on knockout, check out http://knockoutjs.com
source to share
Use System.Text.RegularExpressions (Regex) to find a pattern to replace a table tag:
replace <tr><th>
, and <tr><td>
on the blank,
replace </th></tr>
and </td></tr>
with ^^~~~~~~~~~~^^
means the ending string.
replace </td><td>
with ||^^^^^^^^^||
indicates a metric
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
// ...
// ...
}
source to share