Setting the current value in a DropDownList

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I set the current value in a DropDownList? I populate the DDL and
then I want to position it to the current value.
 
BillG,
How do I set the current value in a DropDownList? I populate the DDL and
then I want to position it to the current value.

There are an endless ways to do that, depending what you want to set in it.

Therefore you should in my opinion explain that first.

Cor
 
I have a ddl called ClassList which has ClassCD for DataValueField and
ClassDesc for DataTextField. I populate the list and then I load the current
member into the web form and I want to select the members classcd in the ddl
to show his/her current classcd
 
Finding the index of an item known to be in a DDL

The question being asked is: we know the list of items in the DDL, so how do we determine the index number of the known item? How do we loop through the items in the DDL to determine the IndexNumber of the item we are looking for? I intend to take that number and use it in
DDL.SelectedIndex = intMyNumber
 
try this ...

Public Function DDLindexOf(ByVal strDDLname As String, ByVal strValue As String) As String

Try

'called by ASP:DropDownLists with DDLindexOf() in a <% %> tag

Dim objDDL As DropDownList

Dim objCellOuter As Control

Dim objCellInner As Control

Dim objCtrl As Control

Dim i As Integer

Dim strIndex As String

Dim bFoundDDL As Boolean = False

'find the value that the DDL should be set to

'by looking in the DataSet for the DataGrid

strValue = objViolationCodeDetails.Tables(0).Rows(masterDataGrid.EditItemIndex).Item(Replace(strDDLname, "ddl", "")).ToString.Trim

'EditItemIndex was set when the editCommand was sent.

'My control name is similar to the column name.

If strValue.Trim.Length = 0 Then

Throw New System.Exception("Column " & Replace(strDDLname, "ddl", "") & " does not exist in " & objViolationCodeDetails.Tables(0).TableName.ToString)

End If

'get a reference to the correct DDL

For Each objCellOuter In masterDataGrid.Controls(0).Controls 'rows

For Each objCellInner In objCellOuter.Controls 'columns

For Each objCtrl In objCellInner.Controls 'controls in the cell, usually asp:labels

If TypeOf objCtrl Is DropDownList Then 'test whether or not the control is a DDL

objDDL = objCtrl

If objDDL.ID = strDDLname Then

bFoundDDL = True 'fast way out of FOR/NEXT

Exit For

End If

End If

Next

If bFoundDDL Then Exit For 'fast way out of FOR/NEXT

Next

If bFoundDDL Then Exit For 'fast way out of FOR/NEXT

Next

If Not bFoundDDL Then ' objDDL should now reference a particular DDL on the

'DataGrid row being edited.

Throw New System.Exception("Cannot locate DDL " & strDDLname)

End If

'cycle through the values in the DDL to find this one, return its index

For i = 0 To objDDL.Items.Count - 1

If objDDL.Items(i).Value.Trim = strValue Then

strIndex = i.ToString

Exit For

End If

Next

If strIndex.Trim.Length = 0 Then

Throw New System.Exception(strValue & " is not a legitimate selected value for " & strDDLname)

End If

Return strIndex 'to use in the <% %> of the ASPX for the <asp:dropdownlist ... selectedindex tag

Catch ex As System.Exception

Throw New System.Exception(ex.Message, ex.InnerException)

End Try

End Function

 
Back
Top