从Javascript/webmethod填充下拉列表 [英] Populate Dropdown list from Javascript / webmethod

查看:95
本文介绍了从Javascript/webmethod填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于在另一个DropDownList上所做的选择填充一个dropdownlist,而无需回发.我使用了cascaingDropDownList并且工作正常.但是,我需要将特定的值加载到这些cascadingdropdownlist中,但我无法做到.因此,我正在寻找一种通过页面上的webmethod或javascript或任何此类方式来实现此目的的方法. Web方法是静态的,不允许直接访问页面控件,因此它本身并不是解决方案.

因此,对这个方向有任何建议的人吗?

I want to populate a dropdownlist based on selection made on another DropDownList, without postback. I used cascaingDropDownList and is working absolutely fine. But, I had a requirement to load a particular value into these cascadingdropdownlists, which I am not able to do. So, I am in search of a way to achieve this through webmethod on the page or javascript or any such means. The webmethod, being static, does not permit access to page controls directly and therefore is not the solution in itself.

So, anyone with any suggestions in this direction?

推荐答案

是的,您可以使用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[^]


这篇关于从Javascript/webmethod填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆