Bind drop down by asp.net using jquery or JSON and web service.
- <asp:DropDownList ID="ddl_country" runat="server"></asp:DropDownList> $(function ()
- {
- BindCountry();
- });
=================Bind country by ajax=============
- function BindCountry()
- {
- $.ajax({
- type: "POST",
- url: "../WebService/MeService.asmx/BindCountry",
- contentType: "application/json; charset=utf-8",
- data: "{ }",
- dataType: "json",
- success: function(Result) {
- var FResult = JSON.parse(Result);
- $("#<%= ddl_country.ClientID %>").append("<option value='0'>-Select One-</option>");
- for (var i = 0; i < FResult.length; i++) {
- $("#<%= ddl_country.ClientID %>").append($("<option></option>").val(FResult[i].Country_Id_Pk).html(FResult[i].Country_Name));
- }
- }
- });
- }
=============================WebMethod====================
- [WebMethod]
- public string BindCountry()
- {
- string output = "";
- SqlCommand cmd3;
- cmd3 = new SqlCommand("WOPS_SP_COUNTRY_STATE_CITY_new", clscode.connect());
- cmd3.CommandType = CommandType.StoredProcedure;
- cmd3.Parameters.Add(new SqlParameter("@callval", SqlDbType.Int)).Value = 19;
- SqlDataAdapter da3 = new SqlDataAdapter(cmd3);
- DataSet ds3 = new DataSet();
- da3.Fill(ds3);
- DataTable dt = ds3.Tables[0];
- output = ConvertDataTabletoString(dt);
- return output;
- }
=====================Now Convert Data Table to String =========================
- public string ConvertDataTabletoString(DataTable dt)
- {
- System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
- List < Dictionary < string, object >> rows = new List < Dictionary < string, object >> ();
- Dictionary < string, object > row;
- foreach(DataRow dr in dt.Rows) {
- row = new Dictionary < string, object > ();
- foreach(DataColumn col in dt.Columns) {
- row.Add(col.ColumnName, dr[col]);
- }
- rows.Add(row);
- }
- return serializer.Serialize(rows);
- }