且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从Javascript/webmethod填充下拉列表

更新时间:2023-02-11 20:26:38

是的,您可以使用Javscript和Web方法填充下拉列表. 使用javascript调用网络方法,该方法将以字符串数组的形式返回将与下拉列表绑定的数据,然后在javascript中将该数据与下拉列表绑定.如下:

yes, You can populate drop down list using Javscript and Web method.
use javascript to call web method which will return data that is to be bind with drop down list in the form of string array, and after that in javascript bind that data with drop down list. as follows:

function bindDdl() {
            try {
                var ddl = document.getElementById('ddl1');
                var x = PageMethods.GetData('', onsuccess, onfail);
            }
            catch (e) {
                alert(e);
            }
        }
        function onsuccess(result) {
            try {
                var ddl = document.getElementById('ddl1');
                var count= ddl.options.length;
                while (ddl.options.length > 0) {
                    ddl.options.remove(0);
                }
                
                for (var i = 0; i < result.length-1; i=i+2) {
                    var opt = document.createElement("option");

                    // Assign text and value to Option object
                    opt.text = result[i];
                    opt.value = result[i+1];
                    ddl.options.add(opt);
                }
            }
            catch (e) {
                alert(e);
            }
        }



GetData(string str)是返回字符串数组的页面方法.



GetData(string str) is the page method which return string array.


选中此项,您会发现自己的需要,


使用jQuery和Web服务基于国家列表选择通过AJAX对城市列表进行过滤 [ ^ ]
Check this, you will find your need,,


Filtering via AJAX for Cities List based on Countries List selection using jQuery and Web Service[^]