テーブルのデータをサーバーに投稿することが目標の場合、通常、テーブルの行をオブジェクトに変換し、ajax() を介してデータを JSON として投稿します。ユーザーがポストバックまたはその他のイベントをトリガーするボタンを押したときに、これを自動的にトリガーするクリック イベントを追加できます。
例:
クライアント側
// Wire up posting the data to the server with a ASP.NET button
$('#<%= Save.ClientID %>').click(function (e) {
PostTable();
});
// Read a row
function GetRow(rowNum) {
var tableRow = $('#partTable tbody tr').eq(rowNum);
var row = {};
row.ChangeType = tableRow.find('td:eq(1)').text();
row.UpdateType = tableRow.find('td:eq(2)').text();
row.Part = tableRow.find('td:eq(5)').text();
row.Price = tableRow.find('td:eq(7)').text();
row.UOM = tableRow.find('td:eq(8)').text();
row.ApplyDate = tableRow.find('td:eq(9)').text();
row.Remarks = tableRow.find('td:eq(10)').text();
return row;
}
// Read all rows
function GetAllRows() {
var dataRows = [];
$('#partTable tbody tr').each(function (index, value) {
var currentRow = GetRow(index);
dataRows.push(currentRow);
});
return dataRows;
}
// POST rows to server
function PostTable() {
var crossId = getParameterByName('id');
var jsonRequest = { rows: GetAllRows(), crossId: crossId };
$.ajax({
type: 'POST',
url: 'TableProcessingViajQueryAjax.aspx/SaveRows',
data: JSON.stringify(jsonRequest),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
success: function (data, text) {
return true;
},
error:function (request, status, error){
return false;
}
サーバ側
[WebMethod]
public static bool SaveRows(List<Row> rows, int crossId)
{
// Do something with your data, maybe put in session/cache/db/etc...
}
サーバー側 (オブジェクトは JS で作成されたオブジェクトと一致する必要があります)
public class Row
{
public string ChangeType { get; set; }
public string UpdateType { get; set; }
public string Part { get; set; }
public double Price { get; set; }
public int UOM { get; set; }
public DateTime ApplyDate { get; set; }
public string Remarks { get; set; }
}
** JSON.ORG の「stringify」を使用して、オブジェクトを有効な JSON オブジェクトに変換しています。