Hi I'm trying to create a list of ListBox's in ASP.NET so that I can then iterate it to test if it has been selected.
List<string> con = new List<string>();
List<ListBox> lb = new List<ListBox>();
foreach (Control c in _pH_Outer_MainCri.Controls)
{
if (c is ListBox)
{
con.Add(c.ID.ToString());
lb.Add(c.??);
}
}
I'm getting caught up on that last part. Basically then I can
foreach(ListBox a in lb)
{
if(a.TestSelection() == true )
{
BuildQue开发者_StackOverflow中文版ry(a);
}
}
Thanks in advance for the help ...
If you are adding all listboxes then you would want
lb.Add((ListBox)c);
You can avoid placing the controls in a list by using LINQ, like this:
var listboxen =
from control in _pH_Outer_MainCri.Controls
where control is ListBox
select control as ListBox;
You can then foreach through the list boxes without having to build an intermediate collection.
When I get overwhelmed, I often find it helpful to take a step back and think about my structures:
List<ListBox> lb
- This is a list and each member in the list is of a type ListBox
.
In your loop, you want to pull out all of the listboxes, so when you have:
if (c is ListBox)
{
you know that c
is already of the type listbox, which is the same type lb
is storing. It is simply a matter of adding c
to your collection in lb
with lb.Add(c);
Separately - do you really need to store these items only to loop through them again and call BuildQuery
? Looping through, putting them in a list, and then looping through that collection again may be extra overhead depending on what you are doing.
精彩评论