Solved

VB.NET - Set the Selected Value of a DropDownList

Posted on 2007-04-04
4
709 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
get column names from table in vb.net 8 30
Expression Evaluater 3 27
Call a function within the ASP code 4 12
Diminish Pop-up  in 3 seconds 7 23
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 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