How to fill a DropDown using Jquery Ajax Call?

前端 未结 4 737
南方客
南方客 2020-12-09 22:20

I have a WebMethod which gets data that I want to fill DropDown with in a DataSet. Currently I am filling the dropdown using a hardcoded object. But I want to replace this

4条回答
  •  情深已故
    2020-12-09 22:55

    [System.Web.Services.WebMethod]
         public static string GetDropDownDataWM(string name)
         {
             //return "Hello " + name + Environment.NewLine + "The Current Time is: "
             //    + DateTime.Now.ToString();
    
             var msg = "arbaaz";
    
             string[] name1 = new string[1];
             string[] Value = new string[1];
             name1[0] = "@Empcode";
             Value[0] = HttpContext.Current.Session["LoginUser"].ToString().Trim();
             DataSet ds = new DataSet();
             dboperation dbo = new dboperation();
             ds = dbo.executeProcedure("GetDropDownsForVendor", name1, Value, 1);
    
             return DataSetToJSON(ds); 
    
         }
    
    public static string DataSetToJSON(DataSet ds)
    {
    
        Dictionary dict = new Dictionary();
        foreach (DataTable dt in ds.Tables)
        {
            object[] arr = new object[dt.Rows.Count + 1];
    
            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                arr[i] = dt.Rows[i].ItemArray;
            }
    
            dict.Add(dt.TableName, arr);
        }
    
        var lstJson = Newtonsoft.Json.JsonConvert.SerializeObject(dict);
        return lstJson;
    }
    

    Ajax Call

    function GetAssociation() {
    
            var myDropDownList = $("#myDropDownLisTId");
            var post_data = JSON.stringify({ "name": "xyz"});
            $.ajax({
                type: "POST",
                url: "test.aspx/GetDropDownDataWM",
                data: post_data,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    json_data = JSON.parse(response.d);
                    myDropDownList.empty();
                    for(i=0; i").val(json_data.Table[i][0]).html(json_data.Table[i][1]));
                    }
    
                },
                failure: function (response) {
                    alert(response.d);
                }
            });
        }
    

提交回复
热议问题