Database
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBoxList ID="CheckBoxList2" runat="server" RepeatColumns="2" RepeatDirection="Horizontal">
<asp:ListItem Text="text1" />
<asp:ListItem Text="text2" />
</asp:CheckBoxList>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns = "3">
<asp:ListItem Text="test" Value="0"></asp:ListItem>
</asp:CheckBoxList>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$(function () {
$.ajax({
type: "POST",
url: "CS.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) {
var xmlDoc = $.parseXML(response.d);
var xml = $(xmlDoc);
var customers = xml.find("Customers");
var repeatColumns = parseInt("<%=CheckBoxList1.RepeatColumns == 0 ? 1 : CheckBoxList1.RepeatColumns %>");
var cell = $("[id*=CheckBoxList1] td").eq(0).clone(true);
$("[id*=CheckBoxList1] tr").remove();
$.each(customers, function (i) {
var row;
if (i % repeatColumns == 0) {
row = $("<tr />");
$("[id*=CheckBoxList1] tbody").append(row);
} else {
row = $("[id*=CheckBoxList1] tr:last-child");
}
var customer = $(this);
$("input", cell).val($(this).find("CustomerID").text());
$("label", cell).html($(this).find("ContactName").text());
row.append(cell);
var html = $("[id*=CheckBoxList1]").html();
cell = $("[id*=CheckBoxList1] td").eq(0).clone(true);
});
}
</script>
</form>
</body>
</html>
Namespace
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
C#
[WebMethod]
public static string GetCustomers()
{
string query = "SELECT top 10 CustomerID, ContactName, City FROM Customers";
SqlCommand cmd = new SqlCommand(query);
return GetData(cmd).GetXml();
}
private static DataSet GetData(SqlCommand cmd)
{
string strConnString = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
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;
}
}
}
}