Barry Michael Doyle
Barry Michael Doyle

Reputation: 10608

Asp.net open DropDownList control in code-behind

I would like to have an <asp:DropDownList> expand when an <asp:Button> is clicked. What function can I use inside the click event to make this happen?

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)

Upvotes: 1

Views: 1262

Answers (1)

Syed Mhamudul Hasan
Syed Mhamudul Hasan

Reputation: 1349

Use Javascript/JQuery to do this. Here's an example:

In the .aspx page

    <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

Upvotes: 1

Related Questions