After gathering information from a web form with Javascript on button_click
, I have stored it in an object. Following this, I made an attempt to utilize Ajax
and JSON
to transmit the object to a method in my C# code.
Javascript:
$(document).ready(function () {
$("#go").click(function () {
$('#<%=gv_Rota.ClientID%>')
.find('tr')
.each(function (row) {
$(this).find('select')
.each(function (col) {
$ctl = $(this)
if ($ctl.is('select')) {
var shift = $ctl.val();
var day = row;
var wk = col + 1;
var objectitem = { ShiftId: shift, Day: day, Week: wk };
$.ajax({
url: '/Administration.aspx/StoreObject',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({ myObject: objectitem })
});
}
});
});
});
});
C# behind:
[WebMethod]
public static void StoreObject(string[] myObject)
{
}
The Javascript function iterates over a GridView to extract the selectedvalues
from DropDownLists within it. This means that the Javascript will execute multiple times, resulting in multiple objects being passed.
How can I store all these passed objects in rows of a DataTable in C#, for instance, so that they can be accessed when I click a 'Save' button? Specifically, I am unsure about what should be included in the StoreArray
method, and secondly, I'm uncertain about the best approach to retain the DataTable after pressing 'Save' and triggering a PostBack
, as it may get reset during this process.