473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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


Nov 17 '05 #1
3 2250
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: huzz | last post by:
How do i make a dropdownlist selected value based on the value i retrive from the database. Basically i have an edit page and like to display the default value in a dropdown list from the...
4
by: Mark Waser | last post by:
I've discovered a very odd bug when attempting to put a dropdown list in a datagrid. In the page PreRender step, the selected index of the datagrid is successfully set during databinding. Yet,...
1
by: jimb | last post by:
I can get the dropdownlist into the datagrid, and I can populate it, but I can't read it. Anybody have a working example of a dropdownlist in an editable grid? Thanks. -- .....
1
by: Vagabond Software | last post by:
I'm using a DropDownList in an EditItemTemplate column. This DropDownList will only ever have two dates in it; Now and the default DateTime value (1/1/1900 12:00:00 AM). Somewhere, somehow, the...
0
by: Mike P | last post by:
How do you get access to the values you have entered under edit mode in a gridview? I only seem to be able to access the values prior to them being edited. protected void...
0
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
1
by: CreativeMind | last post by:
I updated my application from VS2003(.NET framework 1.1) to VS2008(.NET framework 3.5). My form uses a dropdownlist cboLocation. In 1.1 there is no error accessing that form. but when i use 3.5, i...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.