Q: loop for a dropdwon list

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
How can I loop through the items in a dropwdown list: myDDL in C# to see if
there is a string: “xyz�
Thanks,
Jim.
 
Hello JIM.H.,

if (myDropDownList.Items.FindByText("xyz") != null)
{
// the item exists
}
 
Thanks for the reply Matt. I know "xyz" does not exist, but xyz12 or xyz13
may exist, I was thinking if I can loop through the list I may use some kind
of string comparison to find one of those xyz. How can I do that?
 
Jim:
Depending on where you want to do this processing, you could use some
variation of the following two snippets. I have provided a server-side
example in C# and a client-side example in JavaScript.

HTH
----------------
Dave Fancher
http://davefancher.blogspot.com
----------------

[C# Example]
ListItemCollection lic = new ListItemCollection();

foreach(ListItem li in myDDL.Items)
{
if(li.Value.StartsWith("xyz")) // li.Value.IndexOf("xyz", 0) == 0
could be used as well
lic.Add(li);
}

// Do your processing of lic here

----

[JavaScript Example]
var ddl = document.getElementById("myDDL");
for(var i = 0; i < ddl.options.length; i++)
{
var opt = ddl.options;
if(opt.value.indexOf(value) == 0)
{
// Do your processing of opt here
}
}
 
Back
Top