问题描述
我想基于在另一个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?
推荐答案
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.
这篇关于从Javascript/webmethod填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!