数据绑定列表到asp.net下拉控件

时间:2012-05-17 09:15:01

标签: asp.net vb.net drop-down-menu

早上好。 我有一个下拉列表控件,当选择特定项目时,将根据该选择填充另一个下拉列表。背后的代码如下。

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

它正在读取以创建list属性的文件格式如下; 玉米, 小麦, 咖啡

因此,当主下拉列表选项为“期货”时,第二个下拉值将数据绑定到FuturesList属性。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

设置“ddlMain”的AutoPostBack=True,然后设置

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

希望它有所帮助。祝你好运。