Yes nice, however had to change it a tad as the selecteditem property is
read only.

Thanks all who replied

' here ya go..
ddlNames.SelectedIndex =
ddlNames.Items.IndexOf(ddlNames.Items.FindByValue("Value"));


-----Original Message-----
From: dotnet discussion [mailto:[EMAIL PROTECTED]] On Behalf Of
Jim Arnold
Sent: 29 May 2002 10:28
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET] DropDownList


You can also do:

ListItem selectedItem = list.Items.FindByValue(value) list.SelectedIndex
= list.Items.IndexOf(selectedItem)

Jim

> -----Original Message-----
> From: Chris Anderson [mailto:[EMAIL PROTECTED]]
> Sent: 29 May 2002 10:07
> To: [EMAIL PROTECTED]
> Subject: Re: [DOTNET] DropDownList
>
>
> > -----Original Message-----
> > From: Wayne Lee [mailto:[EMAIL PROTECTED]]
> >
> > Yeah but how do I find out the index? When I loop through I am
> > checking the value property of the dropdown against the session
> > value. When a match is found how do I get the index?
>
> Ahh..
> This is what I use: (VB.NET)
>
>     Private Overloads Sub selectByValue(ByVal dropdown As
> DropDownList, _
>                                                 ByVal value As String)
>         Dim index As Integer
>         Dim maxIndex As Integer = dropdown.Items.Count
>
>         Do Until index = maxIndex _
>                         OrElse dropdown.Items(index).Value = value
>             index += 1
>         Loop
>         If index = maxIndex Then
>             'value not found, so select the first one
>             dropdown.SelectedIndex = 0
>         Else
>             dropdown.SelectedIndex = index
>         End If
>     End Sub
>
> (it's overloaded to accept integer,date,etc values..similar code, but
> some casting the value to a string is involved in the other functions)
>
> Merak
>
> You can read messages from the DOTNET archive, unsubscribe from
> DOTNET, or subscribe to other DevelopMentor lists at
> http://discuss.develop.com.
>

You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to