PUZZLE Getting DropDownList Index of Matching Value

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default PUZZLE Getting DropDownList Index of Matching Value

    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 Guest

  2. Similar Questions and Discussions

    1. Dropdownlist index change event problem.
      Hi guys. i can explain for my doubt with example.see.my table have multiple type of employee details like as...
    2. DropDownList is always returning the value from index 0
      I have a DropDownList that is returning the value from index 0 regardless of which item is selected. The code that I am using to test which index it...
    3. Why is CreateChildControls called when a dropdownlist (of a composite control) selected index has changed
      That sums it up. When any of the dependent dropdown lists get their selected index changed (by user interaction) there is a new invocation of the...
    4. puzzle
      8 7 2 9 6 1 8 3 4 2 4 6 2 ? ? 5 who can guess two numbers?
    5. Matching array index to values retrieved from database
      Hello friends, I have this dynamic array(shown below) that I need to match to values (1 - 10) that I am returning from the database via DSN...
  3. #2

    Default Re: PUZZLE Getting DropDownList Index of Matching Value

    This functionality is built into the DropDownList control.

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

    Here's more info:
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolslistitemcollectionclass findbyvaluetopic.asp[/url]

    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolslistitemcollectionclass findbytexttopic.asp[/url]

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


    "Earl Teigrob" <earlt777@hotmail.com> wrote in message
    news:O4YJhoFXDHA.1816@TK2MSFTNGP09.phx.gbl...
    > 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 Guest

  4. #3

    Default Re: PUZZLE Getting DropDownList Index of Matching Value

    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" <Steve@Orr.net> wrote in message
    news:%23V1kTHGXDHA.3444@tk2msftngp13.phx.gbl...
    > This functionality is built into the DropDownList control.
    >
    > Use the myddl.Items.FindByValue(iMyID) method.
    > (or the FindByText method)
    >
    > Here's more info:
    >
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolslistitemcollectionclass findbyvaluetopic.asp[/url]
    >
    >
    [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolslistitemcollectionclass findbytexttopic.asp[/url]
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD
    > [url]http://Steve.Orr.net[/url]
    >
    >
    > "Earl Teigrob" <earlt777@hotmail.com> wrote in message
    > news:O4YJhoFXDHA.1816@TK2MSFTNGP09.phx.gbl...
    > > 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 Guest

  5. #4

    Default Re: PUZZLE Getting DropDownList Index of Matching Value

    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" <earlt777@hotmail.com> wrote in message
    news:O4YJhoFXDHA.1816@TK2MSFTNGP09.phx.gbl...
    > 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 Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139