Asp.net 在代码隐藏中打开 DropDownList 控件

Asp.net open DropDownList control in code-behind

我希望在单击 <asp:Button> 时展开 <asp:DropDownList>。 我可以在点击事件中使用什么功能来实现这一点?

编辑:

服务器端似乎没有功能,如果我必须在客户端进行,根据列表中的项目数,最好的方法是什么? (列表在单击按钮时动态填充)

使用Javascript/JQuery来做到这一点。这是一个例子:

在 .aspx 页面中

    <div>
    <asp:Button ID="btnShowDropDown" runat="server" Text="AddDropdown"  ClientIDMode="Static" />
    </div>
    <div>
        <asp:DropDownList ID="ddlTest" runat="server" ClientIDMode="Static">
          </asp:DropDownList>
    </div>
    <script>
        $(function() {
            $('#btnShowDropDown').on('click', function (e) {
                e.preventDefault();

                //Emty your dropdown list first
                $('#ddlTest').empty();
                //Add first option for validation
                var option = '<option value="">Select</option>';
                $('#ddlTest').append(option);
                //Open dropdown list for size 6
                $('#ddlTest').attr('size', 6);
                //For more add by other data ..Left for your convinent
//                for (var i = 0; i < result.d.length; i++) {
//                  
//                    option = '<option value="' + result.d[i].Id + '">' + result.d[i].SubProductName + '</option>';
//                    $('#ddlTest').append(option);
//                }
            });
        })
    </script>

看到这个link