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)

1
  • 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. Commented Sep 19, 2016 at 11:07

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.