Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dropdownlist preselect item 1

Status
Not open for further replies.

hamking01

Programmer
May 30, 2004
238
US
I have a page with a dropdownlist's list datasouce popoulated with DISTINCT records from a database. The page will allow users to enter new records into database. On the side, I have a search menu where users can search list of records. If they find a desired record, there will be a 'select' button.

I'm trying to have the dropdownlist populate the record that the user selected, but still have all the record options in the dropdownlist. Basically, when a user clicks the 'select' button it'll repopulate the ddl with the record of the selected item.

Can preselected an item from a dropdownlist be done? Any help would be appreciated.

 
Yes it can. What I think is happening is that you are binding the DropDownList on the Page Load so every time the page loads (including when a user selects a record) the bind happens again and therefore the first entry is selected. Try only binding the DropDownList if it is not a postback. e.g
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            ' Bind DropDownList
        End If
    End Sub

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
I've already had the datasource baound in the page.IsPostback. However, when user selects a record from the search feature I've had the ddl poplulated as follows:

ddlStatus.SelectedItem.Text = dt.Rows(0)("Status").ToString

However, this just adds another listitem in the ddl, instead of selected listitem that was bound previously. Can I have it select option already bound to ddl.

For example if ddl datasource has 'Option1' and 'Option2' from Page.IsPostBack. Through search user finds record with 'Option2' and selects record, it'll display the ddl with 'Option2' already selected. With SelectedItem.Text it adds another 'Option2' to the ddl's listitems, resulting in 2 'Option2' in the ddl.

 
Try

ddStatus.items.findbytext(dt.Rows(0)("Status").ToString).selected = true

that always works for me
 
Hamking01,

This is what i've used for all of the applications that i've developed for preselecting a dropdownlist after getting the information from a database. I've edited alot of it out to simplify it some but i hope i included enough so it helps. I tried to add "..." to sections of the code that continue but you should get the basic idea. This may be the long way of doing it, but again, it's always worked without issues so i continue to use it.

Sub getDataBaseValues (byVal myID as String)

If not page.ispostback
Dim myConnection as New SqlConnection...
Dim sql as string = "Select * from table..."
Dim objDR as SQLDataReader
Dim Cmd as New SQLCommand(sql, myconnection)
myConnection.Open()
objDR=Cmd.ExecuteReader...
While objDR.Read()
strWHATEVER=objDR("my_field").tostring
End While
page.databind()
myConnection.Close

Dim myDROPDOWNLIST as listitem
myDROPDOWNLIST = DROPDOWNLIST.Items.FindByValue(strWHATEVER)
If Not myDROPDOWNLIST Is Nothing Then
DROPDOWNLIST.Items.FindByValue (strWHATEVER).Selected = true
End if

End If

End Sub
 
I use these in my utils class:

Public Sub SelectListBoxValue(ByRef MylistBox As Object, ByVal MyVal As Object)
Dim obj As DropDownList = CType(MylistBox, DropDownList)

Dim v As String = CStr(MyVal)
If Not v = Nothing Then v = v.ToLower
Dim i As Integer
For i = 0 To obj.Items.Count - 1
If CType(obj.Items(i), ListItem).Value.ToLower = v Then
obj.SelectedIndex = i
Exit For
End If
Next

End Sub




Public Sub SelectListBoxText(ByRef MylistBox As Object, ByVal MyVal As Object)
Dim obj As DropDownList = CType(MylistBox, DropDownList)

Dim v As String = CStr(MyVal)
Dim i As Integer
For i = 0 To MylistBox.items.count - 1
If CType(MylistBox.items(i), ListItem).Text.ToLower = MyVal.ToString.ToLower Then
MylistBox.selectedIndex = i
Exit For
End If
Next

End Sub

Keeping it Simple
 
One comment on Shatch's solution:

If you try and use that code to set the selected item after its been set in code, you'll get an error saying that a drop list cannot have two selected items...so if you set
Code:
ddStatus.SelectedIndex = -1
before doing the .FindByText, then you'll avoid the error.

D'Arcy

 
Thanks for all the suggestions, I've went with the following:

ddlCurrency.SelectedIndex = ddlCurrency.Items.IndexOf(ddlCurrency.Items.FindByValue(dt.Rows(0)("IdCurrency")))

findbytext didn't work on dropdown's with text more than 1 word.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top