Click here to Skip to main content
15,124,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I how i use Ajax to fill drop-down list using ajax . I am using following code, but it not work for me

C#
<script type="text/javascript" language="jscript">
        function FillValues(ctrl) {

            var sender = ctrl;
            var strCen = '1213';
            var strId = '';

            if (sender == "city") {

                if (strCen.length > 0) {
                    var objAjex;
                    try
                        { objAjex = new ActiveXObject('Msxml2.XMLHTTP'); }
                    catch (e) {
                        try { objAjex = new ActiveXObject('Microsoft.XMLHTTP'); }
                        catch (e2) {
                            try { objAjex = new XMLHttpRequest(); }
                            catch (e3)
                                { objAjex = false; }
                        }
                    }

                    try {
                        var action = "";

                        action = "City";
                        strId = document.getElementById('MainContent_DropDownList1').value;

                        objAjex.onreadystatechange = function () {
                            if (objAjex.readyState == 4) {
                                if (objAjex.status == 200) {
                                    data = objAjex.responseText;

                                    var Dropdown;
                                    if (sender == "city") {
                                        Dropdown = document.getElementById("MainContent_DropDownList2");
                                        Dropdown.disabled = false;
                                        Dropdown.style.display = '';
                                        action = "City";
                                    }
                                    while (Dropdown.childNodes.length >= 1) {
                                        Dropdown.removeChild(Dropdown.firstChild);
                                    }
                                    if (data.length > 0) {
                                        ai = 0;
                                        var value;
                                        var text;
                                        var myArr = data.split("@");
                                        var ddlSelect = document.createElement("OPTION");
                                        if (sender == "city") {
                                            ddlSelect.text = "---------Please Select City---------";

                                        }
                                        ddlSelect.value = "-1";
                                        Dropdown.options.add(ddlSelect);

                                        for (ai = 0; ai <= myArr.length - 1; ai = ai + 1) {
                                            try {
                                                indx = myArr[ai].indexOf('#');

                                                if (indx > 0) {
                                                    if (sender == "city") {
                                                        var ddlSelect = document.createElement("OPTION");
                                                        text = myArr[ai].substring(0, indx);
                                                        ddlSelect.text = text;
                                                        myVal = myArr[ai].substring(indx + 1, myArr[ai].length);
                                                        ddlSelect.value = myVal;
                                                        Dropdown.options.add(ddlSelect);
                                                    }

                                                }
                                            }
                                            catch (e) {
                                                alert(e.message);
                                            }
                                        }
                                    }
                                }
                                else {
                                    alert("Data found with Error");
                                }
                            }
                        }
                        objAjex.open("GET", "Ajax.aspx?Action=" + action + "&Id=" + strId, true);

                        objAjex.send(null);
                    }
                    catch (e) {
                        alert(e.message);
                    }
                }
                else {
                }
            }

        }
    </script>
Posted
Comments
_Maxxx_ 27-Feb-12 22:48pm
   
What does it do? Have you tried debugging the function to see where it fails?
ZurdoDev 28-Feb-12 8:26am
   
Need more info. What is happening?

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