below is my code. this selects the first but 1 and it does not go again
Dim idx As Integer = Me.Dpdtokens.SelectedIndex
If idx <> Me.Dpdtokens.SelectedIndex - 1 Then
Me.Dpdtokens.SelectedIndex = idx 1
Else
Me.Dpdtokens.SelectedIndex = 0
End If
CodePudding user response:
This works for me:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If cboHotel.SelectedIndex >= 0 Then
' user has selected item, increase by 1
If cboHotel.SelectedIndex < cboHotel.Items.Count - 1 Then
cboHotel.SelectedIndex = 1
End If
Else
' no selection at all - lets start at 0
cboHotel.SelectedIndex = 0
End If
End Sub
So, if user not selected anything (and you allowed that, then selected index is -1. However, by default it will start out with the first item as being selected.
It also not clear what/why your logic grabs selected index, and then on next link checks if they are different? (they can't be).
Edit: ==============================
Also, it not been shown how the dropdown/combo box is being loaded up with values.
For example, I have this on page load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadCombo()
End If
End Sub
Sub LoadCombo()
Dim rstHotels As DataTable = New DataTable
Using conn As New SqlConnection(My.Settings.TEST4)
Dim strSQL As String =
"SELECT ID, HotelName, City, Province FROM tblHotels ORDER BY HotelName"
Using cmdSQL As New SqlCommand(strSQL, conn)
conn.Open()
rstHotels.Load(cmdSQL.ExecuteReader)
End Using
End Using
cboHotel.DataSource = rstHotels
cboHotel.DataBind()
End Sub
Note VERY careful how I check for is postback. And thus do not re-load the combo box over and over. If you re-load and re-bind the combo box each time on page load, then the selected index etc. will be lost.
and the above advice applies to gridview, listbox etc. In other words, while we have a page load event, we only want to load up controls on FIRST page load. Since all other buttons and events ALSO WILL trigger page load over and over for each post-back, then if we change the index of the combo box, but re-load each time, then the index location will be lost.
Hence, in general, loading up of such controls like combo box, gridviews etc. should only occur one time, and the first time the page loads. This goal is achieved by checking and using a If Not IsPostBack code stub in the page on-load event.