我想在网格中编辑我的数据。我可以在表格中编辑它并看到它,但我不会去c#方法。我尝试使用ajax但它也不起作用...序列化也不起作用。有什么问题?
[WebMethod]
public static string LoadData()
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("SELECT * from Employees", cn);
DataSet ds = new DataSet();
da.Fill(ds);
return JsonConvert.SerializeObject(ds.Tables[0]);
}
[WebMethod]
public void UpdateGrid()
{
//code to actually do the update to the database
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("SELECT * from Employees", cn);
}
}
和JS:
$(document).ready(function () {
$("#btnEmployee").click(function () {
var service = "Default.aspx/LoadData";
var lastsel2;
$.ajax({
url: service,
datatype: "json",
data: "{}",
contentType: "application/json; charset=utf-8",
method: "POST",
success: function (result) {
result = result.d;
jQuery("#EmpTable").jqGrid({
datatype: "local",
colNames : ['Emp numb', 'First name', 'Second name', 'Salary'],
colModel: [
{ name:"Emp_id", index: "Emp_id", width:80, editable: true},
{ name: "First_name", index: "First_name", width: 80, editable: true},
{ name: "Second_name", index: "Second_name", width: 80, editable: true},
{ name: "Salary", index: "Salary", width: 80, editable: true}
],
data: JSON.parse(result),
rowNum: 10,
loadonce: true,
rowList: [5, 10, 20],
pager: '#EmpPager',
viewrecords: true,
sortorder: 'asc',
gridview: true,
autowidth: true,
sortname: 'Emp_id',
altRows: true,
hoverrows: true,
onSelectRow: function (id) {
if (id && id !== lastsel2) {
jQuery('#EmpTable').restoreRow(lastsel2);
jQuery('#EmpTable').editRow(id, true);
lastsel2 = id;
}
},
caption: "Events",
cellurl: 'Default.aspx/UpdateGrid'
});
$("#EmpTable").jqGrid('navGrid', '#EmpPager', { edit: true, add: true, del: false });
$("#EmpTable").jqGrid('inlineNav', "#EmpPager");
},
error: function (error) {
alert("Oops!");
}
});
});
});