Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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?
Posted
Comments
GeoNav 29-Mar-12 12:47pm    
So, no one has any better suggestion than going in for a postback?

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:

JavaScript
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) is the page method which return string array.
 
Share this answer
 
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900