使用 javascript 更新 asp.net 下拉列表控件

Updating of the asp.net dropdown control using javascript

本文关键字:下拉列表 控件 net asp javascript 更新 使用      更新时间:2023-09-26

在我的 ASP.Net 默认值.aspx中,我有下拉列表控件和以下javascript代码

<asp:DropDownList ID="ddlGenres" runat="server" AppendDataBoundItems="True" AutoPostBack="True" DataSourceID="EntityDataSource2" DataTextField="cityID" DataValueField="cityID" OnSelectedIndexChanged="ddlGenres_SelectedIndexChanged" ClientIDMode="Static" >

<script type="text/javascript">
        var ddlClientID = '<%=ddlGenres.ClientID%>';
        //document.getElementById(ddlGenres)
        function SetddlVal(nIndx)
        {
            //$("#ddlGenres")
            var ddlListSelect = document.getElementById(ddlClientID);
            ddlList.SelectedIndex = nIndx;
        }
    </script>

====

======================================================================

在外部javascript文件中,我调用函数SetddlVal。我调试了代码,并且 ddlList selectedIndex 在脚本中成功更改。

====

=========================================================================

我面临的问题是,当 JavaScript 中的 SelectedIndex 值更改时,ddlGenres_SelectedIndexChanged代码不会被触发。如果我遗漏了什么,请告诉我。谢谢内特

在javascript中,<select>输入的属性selectedIndex不是SelectedIndex

将您的函数更改为以下内容:

function SetddlVal(nIndx)
{
    var ddlListSelect = document.getElementById('<%=ddlGenres.ClientID%>');
    ddlListSelect.selectedIndex = nIndx;
}