Selecting an item in a dropdown list

Last post 11-18-2009 11:41 PM by bluelinenetworks. 9 replies.

Sort Posts:

  • Selecting an item in a dropdown list

    11-11-2009, 5:21 PM
    • Member
      19 point Member
    • bluelinenetworks
    • Member since 09-09-2008, 6:03 AM
    • Alpharetta, GA
    • Posts 96

    Hi all,

     

    I have a dropdown list with 4 items in it.  I want to be able to dynamically choose which one is selected without databinding.

    I have tried:

    For i = 0 To 2
                If cboSD.Items(i).Value = strSchoolType Then cboSD.Items(i).Selected = True
                i = i + 1
            Next


    but I get

    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index 


    Any thoughts? 

    Disclaimer: I am not a pro, and my questions therefore may be quite basic. :D

    Also note, I am very grateful for all the wonderful solutions! (Someday I even hope to provide some!)

    Blue Line Networks
  • Re: Selecting an item in a dropdown list

    11-11-2009, 6:09 PM
    • Contributor
      6,188 point Contributor
    • irokhes
    • Member since 10-29-2009, 5:46 AM
    • Posts 889

    hi,

    yo can select by text or by value, try this

    cboSD.SelectedIndex = cboSD.Items.IndexOf(cboSD.Items.FinByValue(yourValue));

    Or

    cboSD.SelectedIndex = cboSD.Items.IndexOf(cboSD.Items.FinByText(yourText));

    good luck

    Please mark this post as answered if it helped you!

    mY BlOg
  • Re: Selecting an item in a dropdown list

    11-11-2009, 7:07 PM
    • All-Star
      27,770 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,932

    why not base the i on the actual number of items:

    For i As Integer = 0 To cboSD.Items.Count - 1
        If cboSD.Items(i).Value = strSchoolType Then
            cboSD.Items(i).Selected = True
        End If
    Next

    make sure strSchoolType  is of type String. also, you're comparing to Value (vs Text)


    on the other one it is FindByValue:

    DropDownList.SelectedIndex = DropDownList.Items.IndexOf(DropDownList.Items.FindByValue("value"))

    Regards,
    Peter
  • Re: Selecting an item in a dropdown list

    11-12-2009, 11:39 AM
    • Member
      19 point Member
    • bluelinenetworks
    • Member since 09-09-2008, 6:03 AM
    • Alpharetta, GA
    • Posts 96

    turns out that it was just a perverbial blonde moment...

    by using the line i=i+1 it was going outside the range. 

     

    thanks for the input! 

    Disclaimer: I am not a pro, and my questions therefore may be quite basic. :D

    Also note, I am very grateful for all the wonderful solutions! (Someday I even hope to provide some!)

    Blue Line Networks
  • Re: Selecting an item in a dropdown list

    11-12-2009, 11:53 AM
    • All-Star
      27,770 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,932

    that's OK.

    bluelinenetworks:
    by using the line i=i+1 it was going outside the range. 

    as I said, always safer to use a loop based on the number of actual items.


    also, you said there were four items but the counter was going up 3 only - unless that was intended.


    IF it did help though please mark the post(s). thanks!

    Regards,
    Peter
  • Re: Selecting an item in a dropdown list

    11-12-2009, 1:38 PM
    • Member
      19 point Member
    • bluelinenetworks
    • Member since 09-09-2008, 6:03 AM
    • Alpharetta, GA
    • Posts 96

    the going to 3 was just me trying to test, but you are absolutly spot on about using the items.count - 1 

    Disclaimer: I am not a pro, and my questions therefore may be quite basic. :D

    Also note, I am very grateful for all the wonderful solutions! (Someday I even hope to provide some!)

    Blue Line Networks
  • Re: Selecting an item in a dropdown list

    11-18-2009, 5:07 PM
    • Member
      19 point Member
    • bluelinenetworks
    • Member since 09-09-2008, 6:03 AM
    • Alpharetta, GA
    • Posts 96

    Ok, this is working with static lists, but not with databound drop downs?

    When I step through it, the dropdown.items.count is 0?

    I have this in page load and it is not on a post back.... 

    Actually, it works on one, but several others....

    Disclaimer: I am not a pro, and my questions therefore may be quite basic. :D

    Also note, I am very grateful for all the wonderful solutions! (Someday I even hope to provide some!)

    Blue Line Networks
  • Re: Selecting an item in a dropdown list

    11-18-2009, 10:35 PM
    • Member
      19 point Member
    • bluelinenetworks
    • Member since 09-09-2008, 6:03 AM
    • Alpharetta, GA
    • Posts 96

    Just as a note, I have changed it to a linkButton after some research, but same results unless i call dropdown.databind before my code that loads the data and selects the item. 

    Disclaimer: I am not a pro, and my questions therefore may be quite basic. :D

    Also note, I am very grateful for all the wonderful solutions! (Someday I even hope to provide some!)

    Blue Line Networks
  • Re: Selecting an item in a dropdown list

    11-18-2009, 10:39 PM
    • All-Star
      27,770 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,932

    bluelinenetworks:

    Just as a note, I have changed it to a linkButton after some research, but same results unless i call dropdown.databind before my code that loads the data and selects the item. 

    :) I really don't mind you unmarking answers - maybe that was a way to get someone to respond to you - although I think you started out with an unbounded DDL as mentioned in the opening post.

    anyways, how are you going to get an item count unless it is bounded? makes sense right? DDL.DataBind actually creates those items.

    Regards,
    Peter
  • Re: Selecting an item in a dropdown list

    11-18-2009, 11:41 PM
    • Member
      19 point Member
    • bluelinenetworks
    • Member since 09-09-2008, 6:03 AM
    • Alpharetta, GA
    • Posts 96

    Correct on the databinding, but there are 3 dropdowns that I have.  The one that isnt databound has 3 items that I staticly created.  The other 2 are from DB.

    If I do

    page.databind 
    mySub()

    then it works ok, I guess because I am re-binding it? 

    Disclaimer: I am not a pro, and my questions therefore may be quite basic. :D

    Also note, I am very grateful for all the wonderful solutions! (Someday I even hope to provide some!)

    Blue Line Networks
Page 1 of 1 (10 items)