我有跟随在ASPX页面中定义的ListView和一个返回XML字符串的C#webmethod。我想将ListView与webmethod返回的XML数据绑定。另外,我想使用JQuery来执行此绑定。我能够使用JQuery将此XML数据绑定到GridView,但是同样不适用于ListView。请告诉我需要做哪些更改才能实现这一目标。
ASPX页面列表视图 -
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<table class="table" cellspacing="0" cellpadding="3" rules="rows">
<tr class="headerRow">
<th style="width: 40px;">
ID
</th>
<th style="width: 230px;">
Name
</th>
<th style="width: 230px;">
Fee
</th>
</tr>
<tbody>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<div id="testCss">
<tr>
<td>
<span id="id">
<%# Eval("ID") %>
</span>
</td>
<td>
<span id="name">
<%# Eval("Name")%></span>
</td>
<td>
<span id="fee">
<%# Eval("Fee")%>
</span>
</td>
</tr>
</div>
</ItemTemplate>
</asp:ListView>
C#Web方法 -
[WebMethod]
public static string GetCustomers(int pageIndex)
{
string query = "some SQL Query";
SqlCommand cmd = new SqlCommand(query);
return GetData(cmd).GetXml();
}
private static DataSet GetData(SqlCommand cmd)
{
string strConnString = @"some connection string";
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds, "Customers");
return ds;
}
}
}
}
将XML数据绑定到GridView的JQuery函数 - 我想知道如何编写类似的函数来将这个XML数据绑定到上面提到的ListView。
<script type="text/javascript">
$(function () {
GetCustomers(1);
});
function GetCustomers(pageIndex) {
$.ajax({
type: "POST",
url: "JQuery_BindListView_snippetExample.aspx/GetCustomers",
data: '{pageIndex: ' + pageIndex + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var customers = xml.find("Customers");
var row = $("[id*=gvCustomers] tr:last-child").clone(true);
$("[id*=gvCustomers] tr").not($("[id*=gvCustomers] tr:first-child")).remove();
$.each(customers, function () {
var customer = $(this);
$("td", row).eq(0).html($(this).find("Last_Name").text());
$("td", row).eq(1).html($(this).find("First_Name").text());
$("td", row).eq(2).html($(this).find("EMail_Id").text());
$("[id*=gvCustomers]").append(row);
row = $("[id*=gvCustomers] tr:last-child").clone(true);
});
};
</script>
我是JQuery的新手。任何帮助表示赞赏。