ASP.NET Dropdownlist:ddl.text的值变为selectedindexchanged中ddl.selectedvalue的值

时间:2013-03-07 08:42:18

标签: asp.net drop-down-menu selectedvalue selectedindexchanged

ASP.NET 4.0& SQL 2008

我想在我的编码中寻求帮助,我不知道什么是错的或者我错过了什么。 我想要做的是当我在dropdownlist中选择一个项目时,它会向我提供ID (ddlSchool.selectedvalue)和文本(ddlSchool.text)的值。

下面是填充下拉列表的代码。我从数据库中获取了ID和Text的值,它工作得很好;当我调试它时,它会提供正确的ID (newItem.Value = .dr("fnorglevelid"))值和正确的文本值(newItem.Text = .dr("fcorgcode").ToString()

    Protected Sub populateDDLDepartment()

    If Not IsPostBack Then
        Dim newItem As New ListItem()
        newItem.Text = "Select a Department..."
        newItem.Value = "0"
        ddlSchool.Items.Add(newItem)

        Try
            With connSchool
               .conn = New SqlConnection(.strCNN_ADMtoGRAD) : .conn.Open()
               .strSQL = "select fnorglevelid, rtrim(fcorganization) as 'fcorgcode'"& _
                            "from hris_organization " & _
                            "where flschool = 1 " & _
                            "order by fcorgcode asc"
               .cmd = New SqlCommand(.strSQL, .conn)
               .dr = .cmd.ExecuteReader()

                While .dr.Read
                    newItem = New ListItem()
                    newItem.Text = .dr("fcorgcode").ToString()
                    newItem.Value = .dr("fnorglevelid")
                    ddlSchool.Items.Add(newItem)
                End While

            End With
        Finally
            With connSchool
                .conn.Close()
                .conn = Nothing
                .strSQL = vbNullString
                .strSQL = Nothing
                .cmd.Dispose()
                .cmd = Nothing
                .dr = Nothing
            End With
        End Try
    End If
End Sub

但是当我选择一个项目时,ddlSchool.text的值变得与ddlSchool.selectedValue的值相同。我先把它放在消息框中:

    Protected Sub ddlSchool_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSchool.SelectedIndexChanged
         MsgBox("Text:" & ddlSchool.Text & "   ID:" & ddlSchool.SelectedValue)
    End Sub

我错过了什么?我需要做什么?似乎selectedValue与文本相同。

1 个答案:

答案 0 :(得分:2)

您可以使用

<强>值

ddlSchool.SelectedValue

ddlSchool.Text

文字

ddlSchool.SelectedItem.Text for 

谢谢