我使用jquery ajax填充gridview。以下是我使用的代码。
$(document).ready(function(){
$('#gvData').empty();
load_data(0, 10);
});
function load_data(iPageIndex, iPageSize) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "GridAjaxDotnetSpan.aspx/BindEmployees",
data: JSON.stringify({ iPageIndex: iPageIndex, iPageSize: iPageSize }),
dataType: "json",
success: function (result) {
$('#gvData').empty();
$('#gvData').append("<tr><th><input type='checkbox' id='chkOrgheader' name='chkOrgheader' onclick='selectAll();' /></th><th>EmpId </th><th>EmpName </th><th>empsal </th><th>empaddr </th></tr>")
for (var i = 0; i < result.d.length; i++) {
$("#gvData").append("<tr><td><input type='checkbox' id='chkOrgRow' name='chkOrgRow' onclick='selectChildRec(this);' /></td><td>" + result.d[i].EmpId + "</td><td>" + result.d[i].EmpName + "</td><td>" + result.d[i].empsal + "</td><td>" + result.d[i].empaddr + "</td></tr>");
}
},
error: function (result) {
alert("Error");
}
});
}
以下是我的gridview
</asp:GridView>
我上面通过调试,所有行都正确附加到网格(gvData),但网格没有显示在页面上。
请帮忙。
答案 0 :(得分:0)
请试试这个代码。希望这会有效.. !!或者如果您添加了更新面板,请告诉我。
function load_data(iPageIndex, iPageSize) {
try {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "GridAjaxDotnetSpan.aspx/BindEmployees",
data: JSON.stringify({ iPageIndex: iPageIndex, iPageSize: iPageSize }),
dataType: "json",
success: function(result) {
//If you have your data as Json string than here parse your Json string.
result = JSON.parse(result.d);
var tempHtml = "";
if (result) {
var tempheader = "<tr><th><input type='checkbox' id='chkOrgheader' name='chkOrgheader' onclick='selectAll();' /></th><th>EmpId </th><th>EmpName </th><th>empsal </th><th>empaddr </th></tr>";
$("#<%=gvData.ClientId %>").find("thead").html(tempheader);
$("#<%=gvData.ClientId %>").find("tbody").empty();
for (var j = 0; j < result.length; j++) {
tempHtml += "<tr><td><input type=\"checkbox\" id=\"chkOrgRow_" + j + "\" name=\"chkOrgRow_" + j + "\" onclick=\"selectChildRec(this);\" />";
tempHtml += "</td><td>" + result[j].EmpId + "</td><td>" + result[j].EmpName + "</td><td>" + result[j].empsal + "</td>";
tempHtml += "<td>" + result[j].empaddr + "</td></tr>";
}
$("#<%=gvData.ClientId %>").find("tbody").html(tempHtml);
}
},
error: function (result) {
alert("Error");
}
});
} catch (e) {
alert(e.description);
}
}