Solved

VB.NET - Set the Selected Value of a DropDownList

Posted on 2007-04-04
4
714 Views
Last Modified: 2008-02-01
VB.NET, ASP.NET.  .NET 2.0, VB 2005

How do I set a DropDownList to show the correct selected value that I want.  I have a dropdown list that gets populated with a SQLDataSource, but I need it to originally display the correct value, not just the first one returned from the select statement.  

I have the values for DataTextField and DataValueField, but how do I set that to display on the dropdown.

For example, my dropdown has ID B33, B45, and B88 as the DataValueFields and Tim, Suzzie, and Cindy as the matching DataTextField.  

It aways shows up with the first name, Tim.
How can I use a variable that contains the EmpName (or ID) and use it to set what is selected in the dropdown.  None of these worked for me:
Me.ddlDistrict.SelectedValue = EmpName
Me.ddlDistrict.Value = EmpName
Me.ddlDistrict.
0
Comment
Question by:Epicmanagement
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
carmodyk earned 500 total points
ID: 18853255
Public Sub FindItemByID(ByVal cbCombo As ComboBox, ByVal strID As String)
        ' This sub is used to find an Item in a combobox and
        ' set the selected index of the combo box to that item
        Dim bFound As Boolean
        Dim ListItem As ListItem
        Dim ItemCount As Integer

        ListItem = New ListItem

        ItemCount = 0
        While Not bFound And ItemCount <= cbCombo.Items.Count - 1
            ListItem = CType(cbCombo.Items(ItemCount), ListItem)
            If ListItem.ID = strID Then
                cbCombo.SelectedIndex = ItemCount
                bFound = True
            End If
            ItemCount += 1
        End While

        If Not bFound Then
            cbCombo.SelectedIndex = -1
        End If

    End Sub
0
 
LVL 6

Expert Comment

by:carmodyk
ID: 18853269
With the above code, subsitute combobox for dropdownlist.  They function pretty much the same.
0
 

Author Comment

by:Epicmanagement
ID: 18853323
What do I use instead of ListItem ?  

This line gives me an error:
  If ListItem.ID = strID Then

It says ID is not a member of ListItem ?
0
 

Author Comment

by:Epicmanagement
ID: 18853333
Nevermind, I got it.  Thanks.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question