Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on website, I am new to JQuery and Now I am stuck in jquery..
I want to bind all country names in my content page dropdownlist
My Content Page code ,

I removed opening and closing tags as it was not displaying correct in Preview
C#
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server" >

<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.CountryId).html(value.CountryName));
                    });
                },
                error: function (result) {
                    alert('Error');
                }
            });
        });
/script

<asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
   <div>
       <asp:DropDownList ID="ddlCountry" runat="server"></asp:DropDownList>
    </div>

</asp:Content>



My Code File
C#
[WebMethod]
       public static CountryDetails[] BindDatatoDropdown()
       {
           DataTable dt = new DataTable();
           List<countrydetails> details = new List<countrydetails>();

           using (SqlConnection con = new SqlConnection("My Database configuration"))
           {
               using (SqlCommand cmd = new SqlCommand("SELECT CountryID,CountryName FROM Country", con))
               {
                   con.Open();
                   SqlDataAdapter da = new SqlDataAdapter(cmd);
                   da.Fill(dt);
                   foreach (DataRow dtrow in dt.Rows)
                   {
                       CountryDetails country = new CountryDetails();
                       country.CountryId = Convert.ToInt32(dtrow["CountryId"].ToString());
                       country.CountryName = dtrow["CountryName"].ToString();
                       details.Add(country);
                   }
               }
           }
           return details.ToArray();
       }

Above Same code I am able to bind data in Normal forms but not in Nested forms (Means inherited from master page) . Please help me
Posted
Updated 14-Aug-14 2:47am
v2
Comments
Nathan Minier 14-Aug-14 8:39am    
Make sure you're not loading jQuery on both your nested form and on your master; that can cause interesting issues.
RAHUL(10217975) 14-Aug-14 8:41am    
No,I am using JQuery Library in Nested form itself

1 solution

Add ClientIDMode="Static" to your dropdown list Definition. So it looks like
C#
<asp:dropdownlist id="ddlCountry" runat="server" clientidmode="Static" xmlns:asp="#unknown"></asp:dropdownlist>


Regards..
 
Share this answer
 
Comments
RAHUL(10217975) 14-Aug-14 8:54am    
I tried but No Luck
Thanks7872 14-Aug-14 8:57am    
Did you get data in data from method?

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

  Print Answers RSS


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