1

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)

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
  • 2
    You could handle the buttpn's `OnClientClick` event and call `document.getElementById("ddlId").size=10;`. But if you postback it won't stay. So you need to set it also from serverside, f.e. via CSS. – Tim Schmelter Sep 19 '16 at 11:07

1 Answers1

1

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

Community
  • 1
  • 1
Syed Mhamudul Hasan
  • 1,341
  • 2
  • 17
  • 45