i have triend alow to bind data repeater using json but could not get axpacted result
please check the belwo code and tell me if i am doing wrong
the dat is comping broperly but not binding to repeater
aspx:
<%@ Page Title="Contact" Language="C#" AutoEventWireup="true" CodeBehind="Contact.aspx.cs" Inherits="Mimic_Viewer.Contact" %>
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$.ajax({
type: "POST",
url: "Contact.aspx/GetCustomers",
data: '{}',
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) {
$("[id*=dlCustomers]").attr("border", "1");
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var customers = xml.find("Table");
var row = $("[id*=dlCustomers] tr:last-child").clone(true);
$("[id*=dlCustomers] tr:last-child").remove();
$.each(customers, function () {
var customer = $(this);
$(".CustomerID", row).html($(this).find("CustomerID").text());
$(".ContactName", row).html($(this).find("ContactName").text());
$(".City", row).html($(this).find("City").text());
$("[id*=dlCustomers]").append(row);
row = $("[id*=dlCustomers] tr:last-child").clone(true);
});
}
</script>
</head>
<body style="font-size: 10pt; font-family: Arial">
<form id="form1" runat="server">
<asp:Repeater runat="server" ID="rptTest">
<ItemTemplate>
<b>Customer Name:</b>
<asp:Label CssClass="ContactName" runat="server" Text='<%# Eval("ContactName") %>'></asp:Label><br />
<b>Customer ID:</b>
<asp:Label CssClass="CustomerID" runat="server" Text='<%# Eval("CustomerID") %>'></asp:Label>
<b>City:</b>
<asp:Label runat="server" CssClass="City" Text='<%# Eval("City") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
///////////////////////////////////////////////////////////////////////////////////
Aspd.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Mimic_Viewer
{
public partial class Contact : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindDummyRow();
}
}
private void BindDummyRow()
{
//DataTable dummy = new DataTable();
//dummy.Columns.Add("CustomerID");
//dummy.Columns.Add("ContactName");
//dummy.Columns.Add("City");
//dummy.Rows.Add();
//dlCustomers.DataSource = dummy;
//dlCustomers.DataBind();
}
[WebMethod]
public static string GetCustomers()
{
//string query = "SELECT top 10 CustomerID, ContactName, City FROM Customers";
//SqlCommand cmd = new SqlCommand(query);
return GetData().GetXml();
}
private static DataSet GetData()
{
DataSet ds = new DataSet();
DataTable dummy = new DataTable("Customer");
dummy.Columns.Add("CustomerID");
dummy.Columns.Add("ContactName");
dummy.Columns.Add("City");
dummy.Rows.Add(new Object[] { 1, "Smith2", "Singapore"});
dummy.Rows.Add(new Object[] { 2, "Smith3", "Singapore"});
dummy.Rows.Add(new Object[] { 3, "Smith4", "Singapore"});
dummy.Rows.Add(new Object[] { 4, "Smith5", "Singapore"});
dummy.Rows.Add(new Object[] { 5, "Smith6", "Singapore"});
//dummy.Rows.Add(new Object[] { 6, "Smith6", "Singapore", true });
//dummy.Rows.Add(new Object[] { 7, "Smith7", "Singapore", true });
//dummy.Rows.Add(new Object[] { 8, "Smith", "Singapore", true });
//dummy.Rows.Add(new Object[] { 9, "Smith", "Singapore", false });
//dummy.Rows.Add(new Object[] { 10, "Smith", "Singapore", false });
//dummy.Rows.Add(new Object[] { 11, "Smith", "Singapore", false });
ds.Tables.Add(dummy);
//DataTable dt = new DataTable("Pager");
//dt.Columns.Add("PageIndex");
//dt.Columns.Add("PageSize");
//dt.Columns.Add("RecordCount");
//dt.Rows.Add();
//dt.Rows[0]["PageIndex"] = pageIndex;
//dt.Rows[0]["PageSize"] = PageSize;
//dt.Rows[0]["RecordCount"] = ds.Tables[0].Rows.Count;
//ds.Tables.Add(dt);
return ds;
}
}
}