Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Drop Down List behavior
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00940045
Message ID:
00940846
Vues:
16
>If I set cboTarget.SelectedIndex to a numeric value it works correctly - that is it displays the record of interest.
>>
>>Tom

Just FYI

I'm assuming that yor are aware that all arrays, lists (including drop down lists (ddl's) and list boxes), datasets, etc. are all zero based - i.e. the first item is at position zero.

Regarding cboTarget.SelectedIndex, yes it can only be a numeric value and in one environment (can't remember off the top of my head whether it is WebForms or WinForms) it can be -1 to indicate no item selected.

cboTarget.Items.IndexOf( ListItem ) returns numeric

cboTarget.Items.FindByValue( cValueToFind) returns a ListItem, as does FindByText.

Also just FYI, here is the VB.Net code I use to populate combo's preceded by a sample showing typical values in a call.
'Call from WebForm code behind:
Dim cCurrentValue As String = "5"
g.populateCombo( Me.cboDistricts _
		, "SELECT ID, DistrictName FROM Districts WHERE ID > 0" _
		, cCurrentValue _
		, "0", "No Districts Available" _
		, "0", "Please Select District" )


' Sub in Global.asax
Public Sub populateCombo(ByRef cboTarget As DropDownList _
			, ByRef cSQL As String _
			, ByVal cInitialValue As String _
			, ByVal cBlankValue As String, ByVal cBlankDisplay As String _
			, ByVal cFirstValue As String, ByVal cFirstDisplay As String)

	Dim tblTemp As DataTable = Me.getTable(cSQL)

	cboTarget.Items.Clear()
	If tblTemp.Rows.Count = 0 Then
		cboTarget.Items.Add(New ListItem(cBlankDisplay, cBlankValue))
	Else
		If cFirstValue.Length > 0 Then
			cboTarget.Items.Add(New ListItem(cFirstDisplay, cFirstValue))
		End If
		Dim drTemp As DataRow
		For Each drTemp In tblTemp.Rows
			cboTarget.Items.Add(New ListItem(CStr(drTemp(1)), CStr(drTemp(0))))
		Next
		If Not IsNothing(cInitialValue) Then
			cboTarget.SelectedIndex _
				= cboTarget.Items.IndexOf( _
					cboTarget.Items.FindByValue(cInitialValue))
		End If
	End If
End Sub
censored.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform