2014-04-09 22 views
5

DataSet'te DropDown ile doldurmak istediğim verileri alan bir WebMethod var. Şu anda, sabit kodlanmış bir nesneyi kullanarak açılır pencereyi dolduruyorum. Fakat bu sabit kodlanmış nesneyi webmethod tarafından döndürülen verilerle değiştirmek istiyorum.Jquery Ajax Call kullanarak DropDown nasıl doldurulur?

[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 ds.GetXml(); 

     } 

MÜŞTERİ YAN (GÜNCELLEME 1): XML göndermek, direkt DataSet göndermeyen WebMethod itibaren

<script type = "text/javascript"> 
    function GetDropDownData() { 
     var myDropDownList = $('.myDropDownLisTId'); 

     $.ajax({ 
      type: "POST", 
      url: "test.aspx/GetDropDownDataWM", 
      data: '{name: "abc" }', 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (data) { 
       $.each(jQuery.parseJSON(data.d), function() { 
        myDropDownList.append($("<option></option>").val(this['FieldDescription']).html(this['FieldCode'])); 
       }); 
      }, 
      failure: function (response) { 
       alert(response.d); 
      } 
     }); 
    } 
    function OnSuccess(response) { 
     console.log(response.d); 
     alert(response.d); 
    } 
</script> 
+0

cevabınız nasıl görünüyor? –

+0

@Arbaaz, düzenlediğiniz kod (sizin Q'nizde) çalışır mı? – Vikram

cevap

11
function GetDropDownData() { 
    $.ajax({ 
     type: "POST", 
     url: "test.aspx/GetDropDownDataWM", 
     data: '{name: "abc" }', 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function(data.d) 
       { 
        $.each(data.d, function(){ 
         $(".myDropDownLisTId").append($("<option  />").val(this.KeyName).text(this.ValueName)); 
        }); 
       }, 
     failure: function() { 
      alert("Failed!"); 
     } 
    }); 
} 
1

...

[System.Web.Services.WebMethod] 
public static string GetDropDownDataWM(string name) 
{ 
    DataSet ds = new DataSet(); 
    ds.Tables.Add("Table0"); 
    ds.Tables[0].Columns.Add("OptionValue"); 
    ds.Tables[0].Columns.Add("OptionText"); 
    ds.Tables[0].Rows.Add("0", "test 0"); 
    ds.Tables[0].Rows.Add("1", "test 1"); 
    ds.Tables[0].Rows.Add("2", "test 2"); 
    ds.Tables[0].Rows.Add("3", "test 3"); 
    ds.Tables[0].Rows.Add("4", "test 4"); 

    return ds.GetXml(); 
} 

Ajaxönce ara ...

01 (Ajax çağrısı içinde) altına gibi
var myDropDownList = $('.myDropDownLisTId'); 

deneyin ...

success: function (response) { 
    debugger; 

    $(response.d).find('Table0').each(function() { 
      var OptionValue = $(this).find('OptionValue').text(); 
      var OptionText = $(this).find('OptionText').text(); 
      var option = $("<option>" + OptionText + "</option>"); 
      option.attr("value", OptionValue); 

      myDropDownList.append(option); 
    }); 
}, 

Not:

  1. OptionValue ve OptionTextDataSet Tablo Sütunlar bulunmaktadır.

  2. $(response.d).find('Table0').each(function(){}) - İşte Table0 DataSet içindeki Tablo adıdır. Burada

+0

Ancak GetDropDownData() öğesini nerede arayabilirim? Filldd() de doğru mu? – Arbaaz

+0

@Arbaaz Evet, bu 'filldd() işleviyle. –

+0

Kaynak yüklenemedi: sunucu 500 bir durumla yanıt verdi (Dahili Sunucu Hatası – Arbaaz

0
var theDropDown = document.getElementById("myDropDownLisTId"); 
       theDropDown.length = 0; 
       $.each(items, function (key, value) { 

        $("#myDropDownLisTId").append($("<option></option>").val(value.PKId).html(value.SubDesc)); 

"SubDesc", Pktld değerini Veritabanı çıkıyorum anlatılmaktadır. u veri kümesi adresinin değerini ayırmak gerekir.

İlgili konular