Solved

VB.NET - Set the Selected Value of a DropDownList

Posted on 2007-04-04
4
695 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
Comment Utility
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
Comment Utility
With the above code, subsitute combobox for dropdownlist.  They function pretty much the same.
0
 

Author Comment

by:Epicmanagement
Comment Utility
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
Comment Utility
Nevermind, I got it.  Thanks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now