PUZZLE Getting DropDownList Index of Matching Value

Discussion in 'ASP .Net' started by Earl Teigrob, Aug 6, 2003.

  1. Earl Teigrob

    Earl Teigrob Guest

    I want to write a function where I pass in a reference to a dropdownlist and
    a "match value" and have it returns the index of the dropdownlist item that
    matchs (or -1 if there is no match)

    private int GetMatchingIndexOfDropDown(ref DropDownList d, string
    MatchValue)
    {
    return [MatchingIndex]
    }

    I thought it would be super simple but screwed up my first attempt at it.
    I thought that a simple foreach with a counter would solve this and it will
    under some cercomstances...but not all.

    I if use
    ManagerDropDown.Items.Insert(0,new ListItem("--Select--",""));

    to insert a value into a DropDownList after a databind, it gets iterated to
    first but its index value is the largest value. Whoops! Now my simple
    interation does not work!

    FOR EXAMPLE

    private int GetMatchingIndexOfDropDown(ref DropDownList d, string
    MatchValue)

    {

    int i = 0;

    foreach (ListItem li in d.Items)

    {

    if (li.Value == MatchValue)

    return i;

    i++;

    }

    return -1;

    }



    DOES NOT WORK!

    So How Do I Do This??? How Do I get the Index Value of the current List
    Item??? Or am I on the wrong track...

    Thanks

    Earl
     
    Earl Teigrob, Aug 6, 2003
    #1
    1. Advertising

  2. This functionality is built into the DropDownList control.

    Use the myddl.Items.FindByValue(iMyID) method.
    (or the FindByText method)

    Here's more info:
    http://msdn.microsoft.com/library/d...lslistitemcollectionclassfindbyvaluetopic.asp

    http://msdn.microsoft.com/library/d...olslistitemcollectionclassfindbytexttopic.asp

    --
    I hope this helps,
    Steve C. Orr, MCSD
    http://Steve.Orr.net


    "Earl Teigrob" <> wrote in message
    news:...
    > I want to write a function where I pass in a reference to a dropdownlist

    and
    > a "match value" and have it returns the index of the dropdownlist item

    that
    > matchs (or -1 if there is no match)
    >
    > private int GetMatchingIndexOfDropDown(ref DropDownList d, string
    > MatchValue)
    > {
    > return [MatchingIndex]
    > }
    >
    > I thought it would be super simple but screwed up my first attempt at it.
    > I thought that a simple foreach with a counter would solve this and it

    will
    > under some cercomstances...but not all.
    >
    > I if use
    > ManagerDropDown.Items.Insert(0,new ListItem("--Select--",""));
    >
    > to insert a value into a DropDownList after a databind, it gets iterated

    to
    > first but its index value is the largest value. Whoops! Now my simple
    > interation does not work!
    >
    > FOR EXAMPLE
    >
    > private int GetMatchingIndexOfDropDown(ref DropDownList d, string
    > MatchValue)
    >
    > {
    >
    > int i = 0;
    >
    > foreach (ListItem li in d.Items)
    >
    > {
    >
    > if (li.Value == MatchValue)
    >
    > return i;
    >
    > i++;
    >
    > }
    >
    > return -1;
    >
    > }
    >
    >
    >
    > DOES NOT WORK!
    >
    > So How Do I Do This??? How Do I get the Index Value of the current List
    > Item??? Or am I on the wrong track...
    >
    > Thanks
    >
    > Earl
    >
    >
    >
    >
     
    Steve C. Orr, MCSD, Aug 6, 2003
    #2
    1. Advertising

  3. Earl Teigrob

    Earl Teigrob Guest

    Thanks

    I used this to create

    private int GetMatchingIndexOfDropDown(ref DropDownList d, string
    MatchValue)

    {

    d.Items.FindByValue(MatchValue).Selected = true;

    int i = d.SelectedIndex;

    return i;

    }



    "Steve C. Orr, MCSD" <> wrote in message
    news:%...
    > This functionality is built into the DropDownList control.
    >
    > Use the myddl.Items.FindByValue(iMyID) method.
    > (or the FindByText method)
    >
    > Here's more info:
    >

    http://msdn.microsoft.com/library/d...lslistitemcollectionclassfindbyvaluetopic.asp
    >
    >

    http://msdn.microsoft.com/library/d...olslistitemcollectionclassfindbytexttopic.asp
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD
    > http://Steve.Orr.net
    >
    >
    > "Earl Teigrob" <> wrote in message
    > news:...
    > > I want to write a function where I pass in a reference to a dropdownlist

    > and
    > > a "match value" and have it returns the index of the dropdownlist item

    > that
    > > matchs (or -1 if there is no match)
    > >
    > > private int GetMatchingIndexOfDropDown(ref DropDownList d, string
    > > MatchValue)
    > > {
    > > return [MatchingIndex]
    > > }
    > >
    > > I thought it would be super simple but screwed up my first attempt at

    it.
    > > I thought that a simple foreach with a counter would solve this and it

    > will
    > > under some cercomstances...but not all.
    > >
    > > I if use
    > > ManagerDropDown.Items.Insert(0,new ListItem("--Select--",""));
    > >
    > > to insert a value into a DropDownList after a databind, it gets iterated

    > to
    > > first but its index value is the largest value. Whoops! Now my simple
    > > interation does not work!
    > >
    > > FOR EXAMPLE
    > >
    > > private int GetMatchingIndexOfDropDown(ref DropDownList d, string
    > > MatchValue)
    > >
    > > {
    > >
    > > int i = 0;
    > >
    > > foreach (ListItem li in d.Items)
    > >
    > > {
    > >
    > > if (li.Value == MatchValue)
    > >
    > > return i;
    > >
    > > i++;
    > >
    > > }
    > >
    > > return -1;
    > >
    > > }
    > >
    > >
    > >
    > > DOES NOT WORK!
    > >
    > > So How Do I Do This??? How Do I get the Index Value of the current List
    > > Item??? Or am I on the wrong track...
    > >
    > > Thanks
    > >
    > > Earl
    > >
    > >
    > >
    > >

    >
    >
     
    Earl Teigrob, Aug 6, 2003
    #3
  4. Earl Teigrob

    Nedu N Guest

    Earl,

    The same case works perfectly for me..

    ManagerDropDown.Items.Insert(0,new ListItem("--Select--",""));
    this also will insert new item as first with with index value of 0.

    Also you following fuction works for me..
    private int GetMatchingIndexOfDropDown(ref DropDownList d, string
    MatchValue)

    {

    int i = 0;


    foreach (ListItem li in d.Items)

    {

    if (li.Value == MatchValue)

    return i;

    i++;

    }

    return -1;

    }








    "Earl Teigrob" <> wrote in message
    news:...
    > I want to write a function where I pass in a reference to a dropdownlist

    and
    > a "match value" and have it returns the index of the dropdownlist item

    that
    > matchs (or -1 if there is no match)
    >
    > private int GetMatchingIndexOfDropDown(ref DropDownList d, string
    > MatchValue)
    > {
    > return [MatchingIndex]
    > }
    >
    > I thought it would be super simple but screwed up my first attempt at it.
    > I thought that a simple foreach with a counter would solve this and it

    will
    > under some cercomstances...but not all.
    >
    > I if use
    > ManagerDropDown.Items.Insert(0,new ListItem("--Select--",""));
    >
    > to insert a value into a DropDownList after a databind, it gets iterated

    to
    > first but its index value is the largest value. Whoops! Now my simple
    > interation does not work!
    >
    > FOR EXAMPLE
    >
    > private int GetMatchingIndexOfDropDown(ref DropDownList d, string
    > MatchValue)
    >
    > {
    >
    > int i = 0;
    >
    > foreach (ListItem li in d.Items)
    >
    > {
    >
    > if (li.Value == MatchValue)
    >
    > return i;
    >
    > i++;
    >
    > }
    >
    > return -1;
    >
    > }
    >
    >
    >
    > DOES NOT WORK!
    >
    > So How Do I Do This??? How Do I get the Index Value of the current List
    > Item??? Or am I on the wrong track...
    >
    > Thanks
    >
    > Earl
    >
    >
    >
    >
     
    Nedu N, Aug 6, 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. Nathan Sokalski
    Replies:
    2
    Views:
    550
    Juan T. Llibre
    Sep 5, 2005
  2. Nathan Sokalski

    DropDownList always returns value at index 0

    Nathan Sokalski, Sep 18, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    564
    Nathan Sokalski
    Sep 18, 2005
  3. G
    Replies:
    7
    Views:
    148
    Ben Morrow
    Feb 12, 2004
  4. ngoc
    Replies:
    5
    Views:
    188
    Tad McClellan
    May 11, 2006
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    329
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page