By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

PUZZLE Getting DropDownList Index of Matching Value

P: n/a
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


Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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/de...valuetopic.asp

http://msdn.microsoft.com/library/de...ytexttopic.asp

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Earl Teigrob" <ea******@hotmail.com> wrote in message
news:O4**************@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

Nov 17 '05 #2

P: n/a
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" <St***@Orr.net> wrote in message
news:%2****************@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:
http://msdn.microsoft.com/library/de...valuetopic.asp
http://msdn.microsoft.com/library/de...ytexttopic.asp
--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Earl Teigrob" <ea******@hotmail.com> wrote in message
news:O4**************@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


Nov 17 '05 #3

P: n/a
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" <ea******@hotmail.com> wrote in message
news:O4**************@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

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.