Solved

VB.NET - Set the Selected Value of a DropDownList

Posted on 2007-04-04
4
737 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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