Can I set a dropDownList to the TEXT rather than index?

Discussion in 'ASP .Net' started by darrel, May 3, 2004.

  1. darrel

    darrel Guest

    I can set the selected item in a dropDownList to this:

    DropDownList1.SelectedIndex = int

    but is there a way to set it to the text value of the option instead? I've
    looked in the documentation, and it appears the only way to set the selected
    item is to use SelectedIndex.

    -Darrel
    darrel, May 3, 2004
    #1
    1. Advertising

  2. This should work for you:
    DropDownList1.Items.FindByText("foo").Selected = True

    p.s. Make sure you have nothing selected or you will get an error... you
    can't select 2 things at once:
    DropDownList1.SelectedItem.Selected = False

    Garett
    http://www.aimx.com



    "darrel" <> wrote in message
    news:...

    > I can set the selected item in a dropDownList to this:
    >
    > DropDownList1.SelectedIndex = int
    >
    > but is there a way to set it to the text value of the option instead? I've
    > looked in the documentation, and it appears the only way to set the

    selected
    > item is to use SelectedIndex.
    >
    > -Darrel
    >
    >
    Garett Rogers, May 3, 2004
    #2
    1. Advertising

  3. darrel

    Joe Fallon Guest

    Have you tried:
    Me.cboName.SelectedValue = "Sometext"

    Set the Datasource and then databind and then try the syntax above.
    I think the Item collection is not filled until you call databind.
    --
    Joe Fallon



    "darrel" <> wrote in message
    news:...
    > I can set the selected item in a dropDownList to this:
    >
    > DropDownList1.SelectedIndex = int
    >
    > but is there a way to set it to the text value of the option instead? I've
    > looked in the documentation, and it appears the only way to set the

    selected
    > item is to use SelectedIndex.
    >
    > -Darrel
    >
    >
    Joe Fallon, May 4, 2004
    #3
  4. darrel

    darrel Guest

    > Have you tried:
    > Me.cboName.SelectedValue = "Sometext"


    Hmm...well, I've added this:

    CountyData.ReadXml(MapPath("../Judges/JudgeCounty.xml"))
    DropDownList1.DataSource = CountyData
    DropDownList1.DataValueField = "CountyNumber"
    DropDownList1.DataTextField = "CountyName"
    DropDownList1.DataBind()
    -->DropDownList1.SelectedValue =
    (DropDownList1.SelectedItem.text)

    But it doesn't work. Thanks, though! I'll keep digging!

    -Darrel
    darrel, May 4, 2004
    #4
  5. darrel

    darrel Guest

    > DropDownList1.Items.FindByText("foo").Selected = True

    I added it here:

    Try
    CountyData.ReadXml(MapPath("../Judges/JudgeCounty.xml"))
    DropDownList1.DataSource = CountyData
    DropDownList1.DataValueField = "CountyNumber"
    DropDownList1.DataTextField = "CountyName"
    DropDownList1.DataBind()
    -->
    DropDownList1.Items.FindByText(DropDownList1.SelectedItem.text).Selected =
    True

    However, no change. I'm guessing the 'selectedItem' isn't being passed to
    the new page.

    I did find that if I grab the query string instead, THEN it works...so,
    thanks!

    -Darrel
    darrel, May 4, 2004
    #5
    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. Kevin Blount
    Replies:
    2
    Views:
    587
    Kevin Blount
    Aug 23, 2006
  2. Iain
    Replies:
    0
    Views:
    319
  3. mark4asp
    Replies:
    8
    Views:
    6,607
    Andy Dingley
    Mar 17, 2008
  4. Surendra

    Dropdown control that displays images rather than text?

    Surendra, Mar 28, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    119
  5. Replies:
    2
    Views:
    191
    Bob Barrows [MVP]
    Aug 16, 2005
Loading...

Share This Page