Hi,
I have the following code that should create a dropdown when the value in the another dropdown changes. So dropdown2 is created when value of dropdown1 is changed. And dropdown2 is also programatically associated with a datasource which is access in this case.
The dropdown gets created, but for some reason doesn't get populated. What am I doing wrong?

Protected Sub dropdown1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dropdown1.SelectedIndexChanged

        Dim ds As New AccessDataSource
        Dim dropdown2 As New DropDownList
        Dim val As String

        ds.DataFile = "<db path>"
        ds.ID = "ds"
        dropdown2.DataSource = ds
        val = dropdown1.SelectedValue


        If val = "xyz" Then
            dropdown2.DataValueField = "xyz"
            dropdown2.DataTextField = "xyz"
            ds.SelectCommand = "select distinct xyz from Tbl;"
        End If
        form.Controls.Add(dropdown2)
        form.Controls.Add(ds)
    End Sub

Thx.

Dani AI

Generated

When a DropDownList is created at runtime it often appears empty or "loses" its items on the next postback. later reported the problem was resolved, but the underlying causes are common and worth calling out so the fix remains reliable over time. The key points: recreate dynamic controls on every request (before ViewState is restored), give them stable IDs, bind their data after they exist in the control tree, and wire events early.

Checklist to follow every time:

  • Put a PlaceHolder (or panel) in the .aspx markup and add dynamic controls into it rather than directly into the form.
  • Create/recreate the DropDownList in Page_Init (or OnInit), set ID, AutoPostBack and event handlers there.
  • Set DataSource/DataTextField/DataValueField and then call DataBind() after the control is in the control tree.
  • If using an AccessDataSource added programmatically, add that data-source control to the tree before binding and resolve paths with Server.MapPath, ensuring the app pool identity can read the file.

Example pattern (VB) — create/recreate and bind in Init:

Protected Overrides Sub OnInit(e As EventArgs)
    MyBase.OnInit(e)

    ' Recreate control on every request so ViewState and events work
    Dim ddl As New DropDownList()
    ddl.ID = "dynamicDDL"
    ddl.AutoPostBack = True
    AddHandler ddl.SelectedIndexChanged, AddressOf DynamicDdl_SelectedIndexChanged

    placeholder.Controls.Add(ddl)

    ' Provide the data and bind
    ddl.DataSource = GetDataForSelectedValue()   ' return a DataTable/List
    ddl.DataTextField = "Name"
    ddl.DataValueField = "Id"
    ddl.DataBind()
End Sub

Troubleshooting tips: if items still do not appear, confirm DataBind() is called after the control is added; confirm the data source returns rows; use tracing or temporary logging to verify query execution and path resolution (for Access files use Server.MapPath). Marking threads solved (as suggested) is fine once these checks confirm the control is recreated and bound at the right lifecycle stage.

Recommended Answers

All 2 Replies

Nevermind figured it out.

mark as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.