无法从ASP.NET中的AJAX调用获取DropDownList的Text值

时间:2018-02-24 17:21:52

标签: asp.net ajax asp.net-ajax

我无法获取所选DropDownList项的文本值。数据来自对api.geonames.org的AJAX调用。在服务器端,我试图运行SelectedIndexChanged事件来获取文本值并将其存储在字符串变量中。当SelectedIndexChanged运行时,该值始终返回null。

这是AJAX:

$(document).ready(function () {
    GetCountries();
    GetStates();
    GetCities();
});

function GetCountries() {
    $.ajax({
        type: "GET",
        url: "http://api.geonames.org/countryInfoJSON?formatted=true&lang=en&style=full&username=sanjish",
        //url:"http://api.geonames.org/countryInfo?username=sanjish",
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp",
        success: function(data) {
            $(".ddlCustomerCountry").append(
                $('<option />', { value: -1, text: 'Select Country' }));
            $(data.geonames).each(function(index, item) {
                $(".ddlCustomerCountry").append(
                    $('<option />', { value: item.geonameId, text: item.countryName }));
            });
        },
        error: function(data) {
            alert("Failed to get countries.");
        }
    });
}

function GetStates() {
    $(".ddlCustomerCountry").change(function() {
        GetChildren($(this).val(), "States", $(".txtCustomerState"));
    });
}

function GetCities() {
    $(".txtCustomerState").change(function() {
        GetChildren($(this).val(), "Cities", $(".txtCustomerCity"));
    });
};

function GetChildren(geoNameId, childType, ddlSelector) {
    $.ajax({
        type: "GET",
        url: "http://api.geonames.org/childrenJSON?geonameId=" + geoNameId + "&username=sanjish",
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp",
        success: function(data) {
            $(ddlSelector).empty();
            $(ddlSelector).append(
                $('<option />', { value: -1, text: 'Select ' + childType }));
            $(data.geonames).each(function(index, item) {
                $(ddlSelector).append(
                    $('<option />', { value: item.geonameId, text: item.name }));
            });
        },
        error: function(data) {
            alert("failed to get data");
        }
    });
}

这是asp:DropDownList元素的样子:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:DropDownList ID="ddlCountry" runat="server" Width="400px"
            Height="30px" 
            CssClass="ddlCustomerCountry"
            AppendDataBoundItems="true"
            OnSelectedIndexChanged="DdlCountry_OnSelectedIndexChanged">
        </asp:DropDownList>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddlCountry" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
        <asp:DropDownList ID="ddlState" runat="server" Width="400px"
            Height="30px" 
            CssClass="txtCustomerState"
            AppendDataBoundItems="true"
            OnSelectedIndexChanged="DdlState_OnSelectedIndexChanged">
        </asp:DropDownList>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddlState" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>

非常感谢任何帮助!

0 个答案:

没有答案