我在Code Behind中创建JQGRID并尝试调用名为'GetUsersFromSP'的WebMethod,它对我来说很好。我正在收到“loading ...”文本,我正在获取数据。当我点击“加载网格”按钮后,它会回来并再次从网络服务重新加载数据..这是可能的,以便在回发后保留以前的数据,它不应该点击我们的服务。下面是我的代码试过。请帮助我。
protected void Page_Load(object sender, EventArgs e)
{
string myString = "";
myString = "window.onload = function() { ";
myString += "jQuery(document).ready(function() {";
myString += " $('#list').jqGrid({";
myString += " url: 'WebService1.asmx/GetUsersFromSP',";
myString += " data: {},";
myString += " datatype: 'json',";
myString += " mtype: 'POST',";
myString += " ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },";
myString += " serializeGridData: function(postData) {";
myString += " if (postData.filters === undefined) postData.filters = null;";
myString += " if (postData.searchField === undefined) postData.searchField = null;";
myString += " if (postData.searchOper === undefined) postData.searchOper = null;";
myString += " if (postData.searchString === undefined) postData.searchString = null;";
myString += " return JSON.stringify(postData);";
myString += " },";
myString += " jsonReader: {";
myString += " root: function(obj) { return obj.d.rows; },";
myString += " page: function(obj) { return obj.d.page; },";
myString += " total: function(obj) { return obj.d.total; },";
myString += " records: function(obj) { return obj.d.records; }";
myString += " },";
myString += " colNames: ['UserID', 'UserName'],";
myString += " colModel: [";
myString += " { name: 'UserID', index: 'UserID', width: 80, align: 'left', editable: true },";
myString += " { name: 'UserName', index: 'UserName', width: 80, align: 'left', editable: true }],";
myString += " pager: jQuery('#pager'),";
myString += " rowList: [10, 20, 300],";
myString += " rowNum: 10,";
myString += " sortname: 'UserID',";
myString += " sortorder: 'asc',";
myString += " width: 400,";
myString += " viewrecords: true,";
myString += "editurl: 'WebService1.asmx/ModifyData',";
myString += "caption: 'My first grid'";
myString += " }).navGrid('#pager', { edit: true, add: true, del: true, search: true }";
//myString += " { editData: { TableName: Inp1, ColumnValue: myArray} },";
//myString += " { editData: { TableName: Inp1, ColumnValue: myArray} });";
myString += " );";
myString += " jQuery.extend(jQuery.jgrid.edit, {";
myString += " ajaxEditOptions: { contentType: 'application/json' },";
myString += " recreateForm: true,";
myString += " serializeEditData: function(postData) {";
myString += " if (postData.Col1 === undefined) { postData.Col1 = null; }";
myString += " myArray[0] = postData.Col1;";
myString += " myArray[1] = postData.Col2;";
myString += " myArray[2] = postData.Col3;";
myString += " return JSON.stringify(postData);";
myString += " }";
myString += " });";
myString += " jQuery.extend(jQuery.jgrid.del, {";
myString += " ajaxDelOptions: { contentType: 'application/json' },";
myString += " serializeDelData: function(postData) {";
myString += " if (postData.Col1 === undefined) { postData.Col1 = null; }";
myString += " return JSON.stringify(postData);";
myString += " }";
myString += " });";
myString += " });";
myString += "}";
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), myString, true);
}
[的WebMethod] public JQGridResults GetUsersFromSP(string searchField,string searchOper,string searchString,string filters,string _search,string nd,string rows,string page,string sidx,string sord) { int totalRecords = 0; string strSearchCondition = null; Collection users = new Collection(); Hashtable getoperator = new Hashtable();
Hashtable inParams = new Hashtable();
inParams.Add("@CONDITION", strSearchCondition);
inParams.Add("@SORTCONDITION", " order by " + sidx + " " + sord);
inParams.Add("@PAGE", page);
inParams.Add("@ROWS", rows);
inParams.Add("@RECORDCOUNT", totalRecords);
Hashtable outParams = new Hashtable();
outParams.Add("@RECORDCOUNT", totalRecords);
DataSet ds = new DataSet();
int rVal;
ds = GetDataSet(vzaiConn, "SPNAME", out rVal, inParams, out outParams);
totalRecords = (int)outParams["@RECORDCOUNT"];
JQGridResults result = new JQGridResults();
List<JQGridRow> rows1 = new List<JQGridRow>();
if (ds.Tables.Count > 0)
{
foreach (DataRow user in ds.Tables[0].Rows)
{
JQGridRow row = new JQGridRow();
row.id = (int)user["UserID"];
row.cell = new string[6];
row.cell[0] = user["UserID"].ToString();
row.cell[1] = user["UserName"].ToString();
row.cell[2] = user["FirstName"].ToString();
row.cell[3] = user["MiddleName"].ToString();
row.cell[4] = user["LastName"].ToString();
row.cell[5] = user["EmailID"].ToString();
rows1.Add(row);
}
result.rows = rows1.ToArray();
result.page = Convert.ToInt32(page);
result.total = (int)Math.Ceiling((decimal)totalRecords / (decimal)Convert.ToDecimal(rows));
result.records = Convert.ToInt32(totalRecords);
}
return result;
}
答案 0 :(得分:0)
嗨朋友试试这个: -
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
string myString = "";
myString = "window.onload = function() { ";
myString += "jQuery(document).ready(function() {";
myString += " $('#list').jqGrid({";
myString += " url: 'WebService1.asmx/GetUsersFromSP',";
myString += " data: {},";
myString += " datatype: 'json',";
myString += " mtype: 'POST',";
myString += " ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },";
myString += " serializeGridData: function(postData) {";
myString += " if (postData.filters === undefined) postData.filters = null;";
myString += " if (postData.searchField === undefined) postData.searchField = null;";
myString += " if (postData.searchOper === undefined) postData.searchOper = null;";
myString += " if (postData.searchString === undefined) postData.searchString = null;";
myString += " return JSON.stringify(postData);";
myString += " },";
myString += " jsonReader: {";
myString += " root: function(obj) { return obj.d.rows; },";
myString += " page: function(obj) { return obj.d.page; },";
myString += " total: function(obj) { return obj.d.total; },";
myString += " records: function(obj) { return obj.d.records; }";
myString += " },";
myString += " colNames: ['UserID', 'UserName'],";
myString += " colModel: [";
myString += " { name: 'UserID', index: 'UserID', width: 80, align: 'left', editable: true },";
myString += " { name: 'UserName', index: 'UserName', width: 80, align: 'left', editable: true }],";
myString += " pager: jQuery('#pager'),";
myString += " rowList: [10, 20, 300],";
myString += " rowNum: 10,";
myString += " sortname: 'UserID',";
myString += " sortorder: 'asc',";
myString += " width: 400,";
myString += " viewrecords: true,";
myString += "editurl: 'WebService1.asmx/ModifyData',";
myString += "caption: 'My first grid'";
myString += " }).navGrid('#pager', { edit: true, add: true, del: true, search: true }";
//myString += " { editData: { TableName: Inp1, ColumnValue: myArray} },";
//myString += " { editData: { TableName: Inp1, ColumnValue: myArray} });";
myString += " );";
myString += " jQuery.extend(jQuery.jgrid.edit, {";
myString += " ajaxEditOptions: { contentType: 'application/json' },";
myString += " recreateForm: true,";
myString += " serializeEditData: function(postData) {";
myString += " if (postData.Col1 === undefined) { postData.Col1 = null; }";
myString += " myArray[0] = postData.Col1;";
myString += " myArray[1] = postData.Col2;";
myString += " myArray[2] = postData.Col3;";
myString += " return JSON.stringify(postData);";
myString += " }";
myString += " });";
myString += " jQuery.extend(jQuery.jgrid.del, {";
myString += " ajaxDelOptions: { contentType: 'application/json' },";
myString += " serializeDelData: function(postData) {";
myString += " if (postData.Col1 === undefined) { postData.Col1 = null; }";
myString += " return JSON.stringify(postData);";
myString += " }";
myString += " });";
myString += " });";
myString += "}";
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), myString, true);
}
}
这对您没有帮助,请发送清除说明