Looping through controls in ASP.Net C# - c#

I have a page, which contains a table, with a couple of rows, and in that row there are checkboxes.
Now the thing I want, is to loop trough all checkboxes to see if they are checked or not.
This is my current approach:
foreach (Control c in Page.Controls)
{
if(c is Checkbox){
}
}
Now the problem is that I receive only 2 controls, the page and the Table. So the checkboxes are in:
Table -> TableRow -> TableCell -> CheckBox
Is there a way to get ALL controls on a page, instead of having to nest into it to get out the controls?

control.Controls will return the first level child controls only. For details, check this question.

Since the controls are hierarchical, every control sees only it's child controls. You have to iterate through every control to get them all.
You can find an example on how to do it here:
http://www.atrevido.net/blog/CommentView,guid,c792adbf-ce0a-4bf9-a61c-ca1a4296d0ea.aspx

A Control can act as a parent to a collection of controls as in your case .You should Iterate through the child of the parent control you are using in you page.
refer http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls%28VS.71%29.aspx

I just did a nested foreach loop like this:
List<Control> allControls = new List<Control>();
List<string> selectedIDs = new List<string>();
foreach (Control c in this.pnlTable.Controls)
{
allControls.Add(c);
if (c.Controls.Count > 0)
{
foreach (Control childControl in c.Controls)
{
allControls.Add(childControl);
if (childControl.Controls.Count > 0)
{
foreach (Control childControl2 in childControl.Controls)
{
allControls.Add(childControl2);
if (childControl2.Controls.Count > 0)
{
foreach (Control childControl3 in childControl2.Controls)
{
allControls.Add(childControl3);
}
}
}
}
}
}
}
foreach (Control control in allControls)
{
if (control is CheckBox)
{
if (((CheckBox)(control)).Checked)
{
selectedIDs.Add(((CheckBox)(control)).ID);
}
}
}
Depending of the depth of the control I added a if and foreach..
Hope this helps someone else with the same issue...

Related

foreach control c# skipping controls

I have the following loop to remove the buttons in my C# Windows Forms application. The only problem is that it skips every other button. How do I go about removing all the button controls from my form?
foreach (Control cntrl in Controls)
{
if(cntrl.GetType() == typeof(Button))
{
Controls.Remove(cntrl);
cntrl.Dispose();
}
}
I think this way is a bit more readable:
var controlsToRemove = Controls.OfType<Button>().ToArray();
foreach (var control in controlsToRemove)
{
Controls.Remove(control);
cntrl.Dispose();
}
Calling ToArray() makes a new concrete collection, so that you can enumerate over one and modify the other.
Surprised that's not erroring on you, since you're modifying the collection as you're iterating over it. Use a for loop and start at the end:
for (int ii = Controls.Count - 1; ii >= 0; ii--)
{
Control cntrl = Controls[ii];
Controls.remove(cntrl);
cntrl.Dispose();
}
(Starting at the end because otherwise you'd be changing the indexes of each control as you iterated.)
Youre iterating over the same collection from whitch youre removing. Use this code:
List<Control> cleanControls = new List<Control>();
foreach(Control ctr in Controls)
{
if(cntrl.GetType() != typeof(Button))
{
cleanControls.Add(ctr);
}
else
{
ctr.Dispose();
}
}
Controls = cleanControls;
That's It!
Hope I helped!

Default control collection

When I create & add a control to my WinForm using the designer, is my control automatically added to a collection with all the others somewhere ?
Let's say there are like 20 TextBox and I need to clear them all at the same time without calling it like so :
txtbox1.Clear();
txtbox2.Clear();
txtbox3.Clear();
...
I know I should have created manually each control without the designer and add them in a collection but it's too late for that now. So any idea if I can access the whole group of controls ?
try this
private void ClearTextBoxes()
{
Action<Control.ControlCollection> func = null;
func = (controls) =>
{
foreach (Control control in controls)
if (control is TextBox)
(control as TextBox).Clear();
else
func(control.Controls);
};
func(Controls);
}
Oh I actually found out how to do this just after I wrote my question.
I can use a foreach loop on this.controls.
Then I test if the control is a TextBox.
foreach (Control x in this.Controls)
{
if (x is TextBox)
{
((TextBox)x).Text = String.Empty;
}
}

how do you find all DropDownList instances in a page?

the following one doesn't work:
foreach (Control control in Controls) {
if (control is DropDownList) {
DropDownList list = control as DropDownList;
...
}
}
PS: My class extends System.Web.UI.Page
You just need to replace Controls with Form.Controls
foreach (Control c in Form.Controls)
{
if (c is DropDownList)
{
// do something
}
}
Or you can use this extension:
public static IEnumerable<T> AllControls<T>(this Control startingPoint) where T : Control
{
bool hit = startingPoint is T;
if (hit)
{
yield return startingPoint as T;
}
foreach (var child in startingPoint.Controls.Cast<Control>())
{
foreach (var item in AllControls<T>(child))
{
yield return item;
}
}
}
Then you can use it for search of any type of System.Web.UI.Control within specific control. In case of DropDownList you can use it like:
IEnumerable<DropDownList> allDropDowns = this.pnlContainer.AllControls<DropDownList>();
this will find all drop downs within Panel control with ID="pnlContainer".
The problem is that some of the DropDownList controls may be nested in other controls.
If you page has a panel and all of your controls are in that panel the page's control array will only have that panel and all of the control will be in the Panel's control array.
The link that ajax81 linked to will work well.

How to access find all controls and all components into form in C#?

I have a window form which contain some Controls an some Components ( like DataTable, XPCollection etc). I would like find all Control Names and Component Names which used into this form.
You could do,
List<string> ctrlNames = new List<string>();
FIndAllCtrls(ctrlNames , this.Controls);
private void FIndAllCtrls(ctrlNames, ControlCollection ctrlColl)
{
foreach(Control ctrl in ctrlColl)
{
ctrlNames.Add(ctrl.Name);
if(ctrl.Controls.Count > 0)
FIndAllCtrls(ctrlNames, ctrl.Controls);
}
}
IEnumerable<Control> EnumControls(Control top)
{
Queue<Control> todo = new Queue<Control>();
todo.Enqueue(top);
while (todo.Count > 0)
{
Control c = todo.Dequeue();
yield return c;
foreach (Control ch in c.Controls)
todo.Enqueue(ch);
}
}
it is explained in this node:
Find components on a windows form c# (not controls)
It looks there is only way via Reflection available.

How to Find All the Controls in a Form, During the Run Time

Finding all the controls in a form by Using "Reflection"
During the Run Time..
You can recursively iterate through Controls collection on a form or another control.
But you can't get all controls at runtime with reflection if they were added to a controls collection during the program run
List<Control> list = new List<Control>();
GetAllControl(this, list);
private void GetAllControl(Control c , List<Control> list)
{
foreach (Control control in c.Controls)
{
list.Add(control);
if (control.Controls.Count > 0)
GetAllControl(control , list);
}
}
You can do something like this
var controls = from control in this.Controls select control;
or if you want any specific control.
var textboxes = from textbox in this.Controls.OfType<TextBox>()
select textbox;

Categories

Resources