2017-08-03 142 views
0

在Asp.net Webforms我有2个下拉列表从数据库代码隐藏填充。Asp.Net如何填充另一个下拉列表中的下拉列表

我想用第一个ddl过滤器填充第二个ddl。

这里是我的代码

$(function() { 
     $('select#ContentPlaceHolder_ddl1').change(function() { 
      var param1= $(this).val(); 
      var dataString = { 'param1': param1, 'tip' : 1}; 
      //alert(scope); 
      //building unit 
      $('select#ContentPlaceHolder_ddl2').empty(); 

      $.ajax({ 
       url: 'ChangeDDL.aspx/listele', 
       type: 'POST', 
       data: dataString, 
       cache: false, 
       success: function (data) { 
        $.each(data, function (key, DropDownListItem) { 
         $('select#ContentPlaceHolder_ddl2').append(
           '<option value="' + DropDownListItem.value + '">' 
           + DropDownListItem.optionText + 
           '</option>'); 

        }); 
       } 

      }); 

我可以从HTML数据,但我想用DropDownListItem类的数据。我的班级在这里

public class DropDownListItem 
{ 
    public string value { get; set; } 
    public string optionText { get; set; } 
} 

在ChangeDDL.aspx我想返回数据,但我不知道如何。这是我的aspx文件

[System.Web.Services.WebMethod] 
public static List<DropDownListItem> listele(string scope, int tip) 
{ 
    if (tip == 1) 
    { 
     List<DropDownListItem> obj = new List<DropDownListItem>(); 
     DataTable dt = (..sql query) 
       foreach (DataRow dr in dt.Rows) 
     { 
      obj.Add(new DropDownListItem 
      { 
       value = dr["building_unit"].ToString(), 
       optionText = dr["building_unit"].ToString() 
      }); 
     } 
     return obj; 
    } 
    else 
    { 
     return null; 
    } 
} 

我无法访问侦听功能。

回答

1

也许你忘了启用scriptmanager。

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> 
</asp:ScriptManager> 

尝试this tutotial。它解释了如何在ASP.NET中使用AJAX。

+0

如何在我的js代码中使用getSecondDDL。这是足够使用的url:'ChangeDDL.aspx', –

+0

url:“YourNameOfTheASPX.aspx/MethodName”请参阅[this tutotial](https://www.aspsnippets.com/Articles/Call-ASPNet-Page-Method-使用-jQuery-AJAX-Example.aspx)很容易理解 –

+0

我更新了我的问题。 –