简体   繁体   English

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

[英]Asp.net open DropDownList control in code-behind

I would like to have an <asp:DropDownList> expand when an <asp:Button> is clicked.当单击<asp:Button>时,我希望有一个<asp:DropDownList>展开。 What function can I use inside the click event to make this happen?我可以在 click 事件中使用什么函数来实现这一点?

Edit:编辑:

It seems like there's no function to do that server-side, if I have to do it client side, what's the best way to do it according to how many items are in the list?似乎没有功能可以在服务器端执行该操作,如果我必须在客户端执行此操作,根据列表中的项目数量,最好的方法是什么? (the list is populated dynamically on the the button click) (该列表在按钮单击时动态填充)

Use Javascript/JQuery to do this.使用 Javascript/JQuery 来做到这一点。 Here's an example:下面是一个例子:

In the .aspx page在 .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>

See this link看这个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM