Setting DropDown value

Results 1 to 3 of 3

Thread: Setting DropDown value

  1. #1
    Join Date
    Dec 1969

    Default Setting DropDown value

    I&#039;m trying to set a dropdown&#039;s value from what I have stored in a database table. Initially, it doesn&#039;t appear to set correctly, however, when I refresh (F5), the values change to the right setting. All of the text boxes on the screen are populating correctly, but not the dropdowns. Here&#039;s an example:<BR>[code language=""]<BR>&#060;asp:DropDownList ID="Gender" runat=server&#062;<BR> &#060;asp:ListItem Text="Please Select..." value="X"/&#062;<BR> &#060;asp:ListItem Text="Male" value="M"/&#062;<BR> &#060;asp:ListItem Text="Female" value="F"/&#062;<BR>&#060;/asp:DropDownList&#062;<BR>[/code]<BR>Here is where I&#039;m setting the value:<BR>[code language=""]<BR>Gender.SelectedItem.Value = RS("Gender").ToString<BR>[/code]<BR>It almost seems that I need to also set the SelectedIndex in order for this to appear correctly. But, the only way I&#039;m aware of to do that would be to iterate through all of the items until I found the one that matched RS("Gender").ToString, then set the SelectedIndex to that value, but surely there&#039;s another way to get this to work properly. Any ideas?<BR><BR>Thanks,<BR>Jesse<BR>

  2. #2
    Join Date
    Dec 1969

    Default You have to set the selectedIndex.

    Setting the selectedItem&#039;s value is only changing the value, behind the scenes of that first option "Please select..".<BR><BR>To preselect an option on the drop-list, you can do:<BR>

  3. #3
    Join Date
    Dec 1969

    Default try

    [code language="VB.NET"]Dim LI As ListItem = Gender.Items.FindByValue(RS("Gender").ToString))<B R>If Not IsNothing(LI) then LI.Selected = True[/code]<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts