下拉列表值与文本

时间:2014-11-28 10:57:28

标签: vb.net visual-studio-2010

我有一个小问题,有一个DropDownList。此DDL中填充了Page Load中的项目,如下所示:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    Response.Cache.SetCacheability(HttpCacheability.NoCache)

    If Page.IsPostBack = False Then

                ' Fill Activity DDL
                Dim ActivitiesAdapter As New ActivitiesTableAdapters.ActivitiesTableAdapter
                Dim GetActivities As Activities.ActivitiesDataTable = ActivitiesAdapter.GetDataByOnPlantAndOrderType(OrderType, Plant)

                If GetActivities.Rows.Count > 0 Then
                    DDL_Activity_Name.Items.Clear()

                    Dim ActivityList As New List(Of ListItem)
                    ActivityList.Add(New ListItem("Please select ..."))

                    For b = 0 To GetActivities.Rows.Count - 1
                        Dim ActivityRow As Activities.ActivitiesRow = GetActivities(b)
                        Dim ActivityName As String = Nothing
                        Dim ActivityID As Integer = Nothing
                        If ActivityRow.IsNull("ActivityName") = False Then
                            ActivityName = ActivityRow.ActivityName
                        End If
                        If ActivityRow.IsNull("ActivityID") = False Then
                            ActivityID = ActivityRow.ActivityID
                        End If
                        ' First goes the Name then the ID
                        ActivityList.Add(New ListItem(ActivityName, ActivityID))
                    Next

                    DDL_Activity_Name.DataSource = ActivityList
                    DDL_Activity_Name.DataBind()

                End If

    End If
End Sub

页面加载后,用户必须从此DDL中选择一个活动。根据选定的Activity,我填写另一个DropDownList。这是catch:我需要ActivityID(所选项的value属性)。所以我创建了一个SelectedIndex事件:

 Protected Sub DDL_Activity_Name_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DDL_Activity_Name.SelectedIndexChanged

    ' At load page I added for every item a text property (ActivityName), and a value property (ActivityID)
    Dim SelectedActivityID As String = Nothing
    SelectedActivityID = DDL_Activity_Name.SelectedItem.Value

    Dim SelectedActivityName As String = Nothing
    SelectedActivityName = DDL_Activity_Name.SelectedItem.Text

End sub

我的问题是SelectedItem.Value和SelecteItem.Text都返回相同的值。 我检查了页面加载中的代码,我在DDL_Activity_Name中的所有listitems都填充了正确的信息。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

在DDL_Activity_Name上设置DataTextField =" Text"和DataValueField ="价值"

DDL_Activity_Name必须知道存储文本和值的字段。