我在dropdownlist
中添加了项目,当我选择我添加的项目时,它不会显示在Label1
中。这是我的代码:
ASPX
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:DropDownList ID="drpOne" runat="server" AutoPostBack="true">
</asp:DropDownList>
VB
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
con.Open()
If Not IsPostBack Then
Dim Sql = "SELECT College FROM College"
cmdAdd = New SqlDataAdapter(Sql, con)
Dim ds As New DataSet()
cmdAdd.Fill(ds)
drpOne.DataSource = ds
drpOne.DataTextField = "College"
drpOne.DataValueField = "College"
drpOne.DataBind()
drpOne.Items.Insert(0, New ListItem("Please select College", ""))
drpOne.SelectedItem.Value = "Please select College"
drpOne.Items.Insert(0, New ListItem("All", ""))
end if
End Sub
Protected Sub drpOne_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles drpOne.SelectedIndexChanged
Label1.Text = drpOne.SelectedItem.Value
End Sub
答案 0 :(得分:4)
我没有检查过代码,但是这样试试
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
con.Open()
If Not IsPostBack Then
Dim Sql = "SELECT College FROM College"
cmdAdd = New SqlDataAdapter(Sql, con)
Dim ds As New DataSet()
cmdAdd.Fill(ds)
drpOne.AppendDataBoundItems = true
drpOne.Add(New ListItem("Please select College", ""))//You have passed "Please select College" as text and "" as value in ListItem
drpOne.Add(New ListItem("All", ""))
drpOne.DataSource = ds
drpOne.DataTextField = "College"
drpOne.DataValueField = "College"
drpOne.DataBind()
drpOne.Items.FindByText("Please select College").Selected = true
end if
End Sub
Protected Sub drpOne_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles drpOne.SelectedIndexChanged
Label1.Text = drpOne.SelectedItem.Value
End Sub
答案 1 :(得分:1)
drpOne.SelectedItem.Text == "enter the value which you want to search"
答案 2 :(得分:0)
DataValueField必须设置你的主键列Id,我认为CollegeId就是那个。
drpOne.DataTextField = "College"
drpOne.DataValueField = "CollegeId"//Must set Integer value for dropdown item vale