Follow by Email

Thursday, 25 April 2013

How to Bind datalist using Jquery in Asp.Net

ASPX





















































<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: "Default.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:DataList ID="dlCustomers" runat="server" RepeatLayout = "Table" BorderStyle = "Solid">
    <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>&nbsp;
        <b>City:</b> <asp:Label runat="server" CssClass ="City" Text='<%# Eval("City") %>'></asp:Label>
    </ItemTemplate>
 
    </asp:DataList>
         
    </form>
</body>
</html>
Code

























8
















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(cmd).GetXml();
    }
    private static DataSet GetData(SqlCommand cmd)
    {
        string strConnString = ConfigurationManager.ConnectionStrings["conString"].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);
                    return ds;
 
                }
            }
        }
    }

0 comments:

Post a Comment


                                                            

 
Design by Abhinav Ranjan Sinha