1

如何在页面加载时填充 Telerik 下拉列表?

当我尝试填充下拉列表时,出现以下错误(在下面强调的行):

Error: 'data(...)' is null or not an object

这是我尝试填充telerik ddl的方法:

$(function(){
var values = [];
for (var i = 1; i < 10; i++) { 

values.push({ Text: i, Value: i });
}
****$("#MyDdl").data("tDropDownList").dataBind(values);****
});

也尝试过这种方式:

 $(function(){
    onDataBinding();
    });

function onDataBinding(e) {
        var MyDdl = $('#MyDdl').data('tDropDownList');
        var values = [];
        for (var i = 1; i < 10; i++) {  
    values.push({ Text: i, Value: i });
    }
      ****MyDdl.dataBind(values);****
    };

但是在上面强调的行中得到以下未定义的错误:

Error: 'undefined' is null or not an object

注意:添加按钮并在按钮单击事件上加载 ddl 会填充 Telerik 下拉列表。这样做非常好:

    $(function(){
   var values = [];
        for (var i = 1; i < 10; i++) {             
        values.push({ Text: i, Value: i });
        }
        $("#MyDdl").data("tDropDownList").dataBind(values);            
    });

任何帮助深表感谢。

4

2 回答 2

2

您需要在 DropDownList 的 OnLoad 事件处理程序中填充它:

.ClientEvents(events => events.OnLoad("ddl_onLoad").OnChange("ddl_onChange")

处理程序:

function ddl_onLoad(e) {

    var ddl = $(this).data('tDropDownList');

    ddl.dataBind([
        { Text: 'Product 1', Value: '1' },
        { Text: 'Product 2', Value: '2', Selected: true },
        { Text: 'Product 3', Value: '3' },
        { Text: 'Product 4', Value: '4' }
    ]);
}

function ddl_onChange(e) {

    //var ddl = $(this).data('tDropDownList');
    console.log(e.value);
}
于 2012-11-01T22:31:52.087 回答
1

不要直接调用 onDataBinding() 函数..尝试将其链接到控件的客户端事件。

查看

   <%= Html.Telerik().DropDownList()
                          .Name("DropDownList")
                          .ClientEvents(events => events
                          .OnDataBinding("onDropDownListDataBinding"))
        %>

JS

   <script type="text/javascript">
            function onDropDownListDataBinding(e) {
                    var MyDdl = $('#MyDdl').data('tDropDownList');
                     MyDdl .dataBind([
            { Text: "Product 1", Value: "1" },
            { Text: "Product 2", Value: "2", Selected: true },
            { Text: "Product 3", Value: "3" },
            { Text: "Product 4", Value: "4" },
            { Text: "Product 5", Value: "5" }            
        ]);
            };
    </script>
于 2012-10-29T05:45:57.067 回答