DropdownList: How to select by value programaticly?

Discussion in 'ASP .Net' started by Northern, Jul 19, 2003.

  1. Northern

    Northern Guest

    I have an ASP DropdownList populated. What I want is to
    select a new item, in my code, by set a datavale to
    dropdownlist's selectedItem.Value property. I did the
    following:

    MyDropdown.SelectedItem.Value = "value 1"

    Even though the "value 1" is in the dropdownlist's data
    value collection, the dropdown's datatext doesn't change.

    What am I doing wrong here? How should I do this?

    Please help!
    Northern, Jul 19, 2003
    #1
    1. Advertising

  2. Northern

    Rajeev Soni Guest

    You should say

    MyDropdown.SelectedIndex = <Index of the Item you want to select.>

    Rajeev

    "Northern" <> wrote in message
    news:05d001c34da9$49e10000$...
    > I have an ASP DropdownList populated. What I want is to
    > select a new item, in my code, by set a datavale to
    > dropdownlist's selectedItem.Value property. I did the
    > following:
    >
    > MyDropdown.SelectedItem.Value = "value 1"
    >
    > Even though the "value 1" is in the dropdownlist's data
    > value collection, the dropdown's datatext doesn't change.
    >
    > What am I doing wrong here? How should I do this?
    >
    > Please help!
    >
    Rajeev Soni, Jul 19, 2003
    #2
    1. Advertising

  3. Northern

    Northern Guest

    Thank you so much Ken. That's exactly what I need.
    You are the MVP!

    Thank a million!
    >-----Original Message-----
    >Hi,
    >
    >If I understand what you're after, you need to determine

    the index of the value
    >you are seeking and then set the SelectedIndex property

    with that. The example
    >below might be what you need. Let us know?
    >
    >Ken
    >MVP [ASP.NET]
    >
    > Private Sub Button1_Click _
    > (ByVal sender As System.Object, _
    > ByVal e As System.EventArgs) _
    > Handles Button1.Click
    > DropDownList1.SelectedIndex = _
    > DropDownList1.Items.IndexOf _
    > (DropDownList1.Items.FindByValue("Green"))
    > End Sub
    >
    >'---------------------------------------------------------

    ------
    > <form id="Form1" method="post" runat="server">
    > <p>
    > <asp:dropdownlist id="DropDownList1"

    runat="server">
    > <asp:listitem value="Red">Red</asp:listitem>
    > <asp:listitem value="Blue">Blue</asp:listitem>
    > <asp:listitem value="Green">Green</asp:listitem>
    > </asp:dropdownlist></p>
    > <p>
    > <asp:button id="Button1" runat="server"

    text="Select
    >Green"></asp:button></p>
    > </form>
    >
    >
    >"Northern" <> wrote in message
    >news:05d001c34da9$49e10000$...
    >I have an ASP DropdownList populated. What I want is to
    >select a new item, in my code, by set a datavale to
    >dropdownlist's selectedItem.Value property. I did the
    >following:
    >
    >MyDropdown.SelectedItem.Value = "value 1"
    >
    >Even though the "value 1" is in the dropdownlist's data
    >value collection, the dropdown's datatext doesn't change.
    >
    >What am I doing wrong here? How should I do this?
    >
    >Please help!
    >
    >
    >.
    >
    Northern, Jul 19, 2003
    #3
  4. Glad to hear that it worked for you!

    Ken

    "Northern" <> wrote in message
    news:008701c34e22$8c8050e0$...
    Thank you so much Ken. That's exactly what I need.
    You are the MVP!

    Thank a million!
    Ken Cox [Microsoft MVP], Jul 19, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Micheal
    Replies:
    1
    Views:
    835
    Joe Bleaux
    Jul 30, 2003
  2. =?Utf-8?B?UGF0cmljayBG?=

    How to programaticly make the user get the Save the page

    =?Utf-8?B?UGF0cmljayBG?=, Nov 3, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    396
    Bruce Barker
    Nov 3, 2005
  3. SteveSu
    Replies:
    3
    Views:
    543
    Dominick Baier [DevelopMentor]
    Aug 25, 2005
  4. Jason L Lind

    ListBox Programaticly Set Multiple Items as Selected

    Jason L Lind, Apr 11, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    138
    Jason L Lind
    Apr 11, 2006
  5. palmiere
    Replies:
    1
    Views:
    396
    Erwin Moller
    Feb 9, 2004
Loading...

Share This Page