您不应该从 WebMethods 返回 GridViews。您应该返回数据并使用 jQuery 在客户端绑定它。
如果您希望完全替换 GridView,我建议您使用某种 jQuery 插件以表格方式显示数据。你可以查看jQGrid 或datatables(我最喜欢的)。您的 Web 方法只能返回 Json 格式的数据。类似的东西:
[WebMethod]
public List<CustomObject> GetData(string param1, string param2)
{
//return data here
}
在数据表的特定情况下,您必须遵守an interface。在我的 C# 版本上看起来像这样:
public class ResponseData
{
#region properties
public int iTotalRecords { get; set; } //used by datatables to build the pager
public int iTotalDisplayRecords { get; set; } //used by datatables to build the pager
public string sEcho { get; set; }
public string sColumns { get;set; } //optional
public List<CustomObject> aaData { get; set; } //your actual business objects
#endregion
}
所以你的网络方法,如果你选择使用数据表,应该返回ResponseData
[WebMethod]
public ResponseData GetData(string param1, string param2)
{
//return ResponseData
}
您可以在客户端绑定数据, 这样做:
$(document).ready(function() {
var oTableSummary = $("#tbl").dataTable({
"bJQueryUI": true,
"bPaginate": false,
"sPaginationType": "full_numbers",
"bServerSide": true,
"bProcessing": true,
"iDisplayLength": 50,
"bFilter": false,
"bSortClasses": false,
"bDeferRender": false,
"oLanguage": { "sSearch": "Search all columns:" },
"aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
"sAjaxSource": "WebService.asmx/GetData",
"fnServerData": function(sSource, aoData, fnCallback) {
$.ajax({
"type": "POST",
"dataType": 'json',
"contentType": "application/json; charset=utf-8",
"url": sSource,
"data": "{'param1': 'param1" , 'param2': 'param2' }",
"success": function(result) {
fnCallback(result); // draw the table
}
});
}
});
});
PS:你需要花很多时间来学习这些东西,但如果你掌握了它,你就不想再回到使用服务器控件了。 :D