Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8939799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:54:00+00:00 2026-06-15T10:54:00+00:00

Trying to Populate DropDownList when it is clicked (Asp.Net). I have a sample below

  • 0

Trying to Populate DropDownList when it is clicked (Asp.Net). I have a sample below but I can’t get the DropDownList to stay expanded after it is loaded. Does anyone have a solution to load on demand from server other than using 3rd party controls?

JavaScript

<script type="text/javascript">
    function LoadOnDemand(ddl) {
        if (ddl != "") {
            var control = document.getElementById(ddl.id);
            __doPostBack(ddl.id, "LoadOnDemand");
        }
    }
    function AfterLoadOnDemand(ddl) {
    }

</script>

Markup

<body>
    <form id="form1" runat="server">
        <div>
            <asp:Panel ID="DynamicPanel" runat="server" Width="200">
            </asp:Panel>
        </div>
    </form>
</body>

CodeBehind

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim target As String = Page.Request.Params.Get("__EVENTTARGET")
    Dim eventarg As String = Page.Request.Params.Get("__EVENTARGUMENT")

    Call CreateDynamicControls()

    If IsPostBack Then
        If target <> "" Then
            If eventarg = "LoadOnDemand" Then
                Dim ctrl As Control = FindControl(target)

                If ctrl IsNot Nothing Then
                    If TypeOf ctrl Is DropDownList Then
                        'Note: values would be from a database or table query. 
                        '      and some other logic. Simplified here.
                        Dim ddl As DropDownList = CType(ctrl, DropDownList)
                        ddl.Items.Clear()
                        ddl.Items.Add("one")
                        ddl.Items.Add("two")
                        ddl.Items.Add("three")

                        'remove onfocus from this control so it doen't fire again 
                        'if they click it immediatly after loading
                        ddl.Attributes.Add("onfocus", "AfterLoadOnDemand(this)")
                    End If

                    'reset the LoadOnDemand for all the other DropDownList controls
                    For Each notFocusedControl As Control In DynamicPanel.Controls
                        If TypeOf notFocusedControl Is DropDownList Then
                            Dim ddl As DropDownList = CType(notFocusedControl, DropDownList)
                            If ddl.ID <> target Then
                                ddl.Attributes.Add("onfocus", "LoadOnDemand(this)")
                            End If
                        End If
                    Next
                End If

            End If
        End If
    End If

End Sub

Protected Sub CreateDynamicControls()
    For i As Integer = 0 To 2
        Dim ddl As New DropDownList
        ddl.ID = "DropDownList" & i
        ddl.Width = 150
        ddl.Items.Add("Browse me..")
        ddl.Attributes.Add("onfocus", "LoadOnDemand(this)")
        ddl.AutoPostBack = True
        ddl.EnableViewState = True
        DynamicPanel.Controls.Add(ddl)
        DynamicPanel.Controls.Add(New LiteralControl("<br/><br/>"))
    Next
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:54:01+00:00Added an answer on June 15, 2026 at 10:54 am

    The only way I found out how to exand it is by emulation. Here is a link (ExpandSelect) that has a JavaScript function to do exactly that.
    And here is another reference link.

    I added the following (initialWidth parameter) to the .js file. This keeps the expanded dropdown from shrinking smaller then the initial width of the dropdown.

    function ExpandSelect(select, maxOptionsVisible, initialWidth) {
    
        //code - see file
        select.style.zIndex = "1000000";
    
        //Added this right after the zIndex
        if (dropWidth < initialWidth) {
            select.style.width = initialWidth + 'px';
            select2.style.width = initialWidth + 'px';
        }
    
        //code - see file
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# ASP.NET web application an I am trying to populate an
I am trying to populate asp:DropDownList with data items and here is the code
We're generating a dropdownlist in ASP.NET repeater as below - <asp:DropDownList ID=ddlQty runat=server> <asp:ListItem
I have an asp.net dropDownList which is automatically bound to a sqlDataSource to values
ASP.Net MVC 4 I am trying to populate a list of Countries (data from
I trying to populate the dropdown list when page was loaded.But it is not
I am just trying to populate a html.dropdown list using mvc2 in VS2008. But
I am trying to populate a ListView ....but I cant add 2 string values
I am trying to populate a spinner, but am getting an error. Here is
I'm trying to populate a DropDownList with values pulled from a property, and my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.