Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello...
im trying to bind data from sqlserver database to dropdownlist placed in content/child page using jquery. I referred many articles, they specified only for plain webform, not to content pages.
Its working fine in plain webform, and not in child pages...! my code is :

XML
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Jquery_DDLsample.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "WebForm1.aspx/BindDatatoDropdown",
            data: "{}",
            dataType: "json",
            success: function (data) {
                $.each(data.d, function (key, value) {
                    $("#ddlCountry").append($("<option></option>").val(value.CatId).html(value.CatName));
                });
            },
            error: function (result) {
                alert("Error");
            }
        });
    });
</script>
<div>
<asp:DropDownList ID="ddlCountry" runat="server" />

    <br />
    <br />
</div>
</asp:Content>


c# code:
[WebMethod]
public static CatDetails[] BindDatatoDropdown()
{
DataTable dt = new DataTable();
List<catdetails> details = new List<catdetails>();

using (SqlConnection con = new SqlConnection("Data Source=COMPUTER34;Initial Catalog=ContentOptDB;user id=sa;password=unlock"))
{
using (SqlCommand cmd = new SqlCommand("SELECT * from tblCategories", con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dtrow in dt.Rows)
{
CatDetails cat = new CatDetails();
cat.CatId = Convert.ToInt32(dtrow["CatId"].ToString());
cat.CatName = dtrow["CatName"].ToString();
details.Add(cat);
}
}
}
return details.ToArray();
}
public class CatDetails
{
public int CatId { get; set; }
public string CatName { get; set; }
}

Any help appreciated...!
Thank you...
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900