dinotom
dinotom

Reputation: 5162

databinding list to asp.net dropdown control

Good Morning. I have a dropdown list control which when a particular item is selected, another dropdown's list is populated based on that selection. The code behind is below.

Imports System.IO
Partial Class Data
Inherits System.Web.UI.Page
Private Property _futureslist As List(Of String)
Property FuturesList As List(Of String)
    Get
        Return _futureslist
    End Get
    Set(value As List(Of String))
        Dim strText As String =    File.ReadAllText(HttpContext.Current.Server.MapPath("~/DataFiles/AvailableFutures.txt"))
        Dim arytext() As String = strText.Split(",")
        For i As Integer = 0 To arytext.Count - 1
            value.Add(arytext(i))
        Next
        _futureslist = value
    End Set

End Property
Protected Sub ddlMain_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlMain.SelectedIndexChanged
    If Not IsPostBack Then

        If ddlMain.SelectedItem.Text = "Futures" Then
            ddlMainsub1.DataSource = _futureslist
            ddlMainsub1.DataBind()

        End If
    End If
End Sub
End Class

The file it's reading to create the list property is formatted as follows; Corn, Wheat, Coffee

So when the main dropdown selected item is "Futures" the second dropdown values are databound to the FuturesList property. Any help would be appreciated

Upvotes: 0

Views: 745

Answers (1)

Krishanu Dey
Krishanu Dey

Reputation: 6406

set AutoPostBack=True of "ddlMain" and then at

Protected Sub ddlMain_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlMain.SelectedIndexChanged
 ddlMainsub1.Items.Clear()
 'bound items here to DropDownList2
End Sub  

Hope It Helps. Good Luck.

Upvotes: 1

Related Questions