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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SelectedIndex in a DropDownList from a value 1

Status
Not open for further replies.

blueark

Technical User
Apr 16, 2002
1,212
IE
I've got a DropDownList that's populated dynamically. Values are set to unique ID's from a database, and the text is a corresponding string from the same database row.

If I know what ID value should be selected, how do I go about setting the SelectedIndex property of the list? For example, the list might be populated with five or six items, and all I know is that one of them has a value of 1354. How do I make sure that it is selected without knowing where in the list it is going to show up? I'm sure it's something very simple, as always!
 
One option is to iterate through listitems, like:

Code:
dim alistitem as listitem
for each alistitem in mydropdownlist.listitems
if alistitem.value = myvaluevariable then
     mydropdownlist.selecteditem = alistitem
end if
next

let me know if that is not what you wanted. -b

 
Or you can use:

MyDropDownList.Items.FindByValue(YourValueHere).Selected = true;

You need to put it in a try - catch construct, though, because if it doesn't find what you're looking for, it will throw an error. Just set the .SelectedIndex = 0 in the catch block.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Perfect, thanks people! You wouldn't believe how long I've been looking for that piece of information!
 
i've stripped down the code until i get this problem worked out. here's what i've been using for addminiagenda.ascx:
Code:
<%@ Control Language=&quot;VB&quot; %>
<%@ import Namespace=&quot;system.data&quot; %>
<%@ import Namespace=&quot;system.data.oledb&quot; %>
<script runat=&quot;server&quot;>
dim intmemberid as integer
Public Property memberid() As integer
   Get
      Return intmemberid
   End Get
   Set(ByVal Value As integer)
      intmemberid = Value
   End Set
End Property
</script>
<table>
    <tbody>
        <tr>
            <td>
                Meeting Date:</td>
            <td>
                <asp:dropdownlist id=&quot;dddates&quot; runat=&quot;server&quot;></asp:dropdownlist>
            </td>
        </tr>
        <tr>
            <td valign=&quot;top&quot;>
                Agenda:</td>
            <td>
                <asp:textbox id=&quot;tbminiagenda&quot; runat=&quot;server&quot; Width=&quot;400px&quot; Height=&quot;150px&quot;></asp:textbox>
            </td>
        </tr>
    </tbody>
</table>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top