下面是我的代碼。這選擇了第一個但 1 并且它不會再去
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
uj5u.com熱心網友回復:
這對我有用:
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
因此,如果用戶未選擇任何內容(并且您允許這樣做,則所選索引為 -1。但是,默認情況下,它將從第一項被選中開始。
它也不清楚什么/為什么你的邏輯抓取選定的索引,然后在下一個鏈接檢查它們是否不同?(他們不能)。
編輯:==============================
此外,還沒有顯示下拉/組合框是如何加載值的。
例如,我在頁面加載時有這個:
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
注意我檢查回發的方式非常小心。因此不要一遍又一遍地重新加載組合框。如果每次在頁面加載時重新加載并重新系結組合框,則所選索引等將丟失。
上面的建議適用于 gridview、listbox 等。換句話說,雖然我們有一個頁面加載事件,但我們只想在第一個頁面加載時加載控制元件。由于所有其他按鈕和事件也將在每次回發時一遍又一遍地觸發頁面加載,那么如果我們更改組合框的索引,但每次都重新加載,那么索引位置將丟失。
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.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/347476.html
上一篇:加載場景未正確重啟場景
