从数据库&获取RadioButtonList值选

时间:2016-05-05 09:43:40

标签: asp.net vb.net

我正在尝试从数据库填充My RadioButtonList值。这对我有用。但我不知道如何在数据库中保留一个预定义的值,默认情况下在radiobuttonList中选择。我的db表中有城市列表。我想在页面加载时选择一个城市。以下是我的代码

Private Sub MasterPage_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.PopulateCities()
End Sub

Private Sub PopulateCities()
        Using conn As New MySqlConnection()
            conn.ConnectionString = ConfigurationManager _
                .ConnectionStrings("conio").ConnectionString()
            Using cmd As New MySqlCommand()
                cmd.CommandText = "Select cityName from cities where status = 'active' order by cityName"
                cmd.Connection = conn
                conn.Open()
                Using sdr As MySqlDataReader = cmd.ExecuteReader()
                    While sdr.Read()
                        Dim item As New ListItem()
                        item.Text = sdr("cityName").ToString()
                        item.Value = sdr("cityName").ToString()
                        item.Selected = Convert.ToBoolean(sdr("IsSelected"))
                        locationSelector.Items.Add(item)
                    End While
                End Using
                conn.Close()
            End Using
        End Using

1 个答案:

答案 0 :(得分:0)

如果您的数据库cities表包含IsSelected字段,那么您需要像这样更改您的查询

Select cityName, IsSelected from cities where status = 'active' order by cityName

您需要更新第一个列表项

locationSelector.Items(0).Selected = True