I want to iterate through various controls of update panel. While iterating using ID for control I want to delete some of the controls.
But I don't know how to iterate through controls of update panel using GetEnumerator method?
If we can do iteration by some other way, please let me know.
Couldn't you loop over the Controls collection of the updatepanel:
foreach(var control in myUpdatePanel.Controls) {
...
}
You can loop the ControlCollection.
Just remember that these controls can be nested, if they are in panels eg.
private void RecusiceControls(ControlCollection controls)
{
foreach (Control control in controls)
{
RecusiceControls((ControlCollection)control.Controls);
if (control is Button) // whatever the control is you are looking for
{
}
}
}
Related
First, I read this Reset all the items in a form
It was a great help until I realised all my controls are inside a TabControl containing itself several tabs in which there are all the common controls i.e. textbox, datetimepicker, datagrigview, etc....
Then I tried MyTabControl.Controls.Clear() but this deleted all tabs in the form.
How can I implement this Reset all the items in a form in a TabControl ?
use:
foreach (Control c in GetAll(myTabControl))
{
ResetAllControls(c);
}
in which ResetAllControls is the method in your referenced link and
public static IEnumerable<Control> GetAll(Control control)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl))
.Concat(controls);
}
from the accepted answer of this question.
This question already has answers here:
Method to reset members inside groupBox
(3 answers)
Closed 6 years ago.
I wanna clean my groupbox items after i click an button.
I tried some code blocks, but they don´t work for reset controls.
I don´t wanna remove or delete it, I just wanna reset the items in the groupbox.
This is working for remove groupbox items.
public void ClearPanels(GroupBox control)
{
control.Controls.Clear();
}
or this
groupBox2.Controls.Clear();
It looks like this, before click.
And when I click the button, as you can see on the right side.
It is removed, but I want to reset it.
Any ideas how I can do this ?
I'm going to asume that clear means to leave it all by default. You need to llop all the controls inside the groupbox and depending on what control they are do something or something else.
foreach (Control ctr in GB.Controls)
{
if (ctr is TextBox)
{
ctr.Text = "";
}
else if (ctr is CheckedListBox)
{
CheckedListBox clb = (CheckedListBox)ctr;
foreach (int checkedItemIndex in clb.CheckedIndices)
{
clb.SetItemChecked(checkedItemIndex, false);
}
}
else if (ctr is CheckBox)
{
((CheckBox)ctr).Checked = false;
}
else if (ctr is ComboBox)
{
((ComboBox)ctr).SelectedIndex = 0;
}
}
I dont know what Deneyim and Not are, but i guess you get the idea of checking what it is and asigning the value you want
I think the term clear depends a little on the kind of controls you use. While for a TextBox you probably want to empty the Text property, for a ListBox you probably want to remove all items. So there is no common Clear() method for controls.
What you can do is something like that:
public void ClearPanels(GroupBox control)
{
foreach(Control childControl in control.Controls)
childControl.ResetText();
}
This iterates through all child Controls in your GroupBox and resets the Text property of them. You may wish to add special treatment for certain types of Control.
If your GroupBox contains nested controls (such as another GroupBox with further controls on it), you may need to make this method recursive:
public void ClearPanels(Control control)
{
foreach(Control childControl in control.Controls)
{
childControl.ResetText();
ClearPanels(childControl); // recursive call
}
}
To really "clear" your controls, you have to check their specific type. So a little advanced method could be this:
public void ClearPanels(GroupBox control)
{
foreach(ListBox listBox in control.Controls.OfType<ListBox>())
{
listBox.Items.Clear();
// do more ListBox cleanup
}
foreach(CheckedListBox listBox in control.Controls.OfType<CheckedListBox>())
{
listBox.Items.Clear();
// do more CheckedListBox cleanup
}
foreach(ListView listView in control.Controls.OfType<ListView>())
{
listView.Items.Clear();
// do more ListView cleanup
}
foreach(CheckBox checkBox in control.Controls.OfType<CheckBox>())
{
checkBox.Checked = false;
// do more CheckBox cleanup
}
// etc...
}
I'm trying to perform an operation on every control on a page that is inherited from a masterpage. I don't know how to access the child pages controls. I have tried recursively getting to my controls like this:
private void checkControls(ControlCollection controlcollection)
{
foreach (Control control in controlcollection)
{
if (control.Controls.Count > 0)
{
Debug.WriteLine(control.GetType().ToString());
checkControls(control.Controls);
}
else
{
Debug.WriteLine(control.GetType().ToString());
}
}
The method is called like this:
protected void resettodefault()
{
checkControls(this.Page.Controls);
}
However, the only controls that are printed from this execution are:
ASP.site_master
System.Web.UI.LiteralControl
I would prefer to access my controls directly (without recursion). Otherwise, how can I modify my recursion to get to the desired page's controls?
I would suggest using a base page instead of a master page, this way your logic for iterating over controls (and whatever you will do with that afterwards) is not tied to which master page a page is using.
As far as getting all the controls on the page, because the controls are hierarchical, as is the HTML they represent, so iterating over them recursively makes sense. However if you are dead set on not recursively getting controls something like this should work:
IEnumerable<Control> GetAllControls()
{
var allControls = new List<Control>();
var currentControls = new Queue<Control>();
currentControls.Enqueue(this.Page);
while (currentControls.Count >0)
{
var c = currentControls.Dequeue();
if (!allControls.Contains(c))
{
allControls.Add(c);
if (c.Controls != null && c.Controls.Count > 0)
{
foreach (Control e in c.Controls)
{
currentControls.Enqueue(e);
}
}
}
}
return allControls;
}
The last thing to consider is the lifecycle of the page, and when you iterate over the controls. If you try to walk to control tree too early not all controls may exist.
EDIT: Updated code.
Update
For validation purposes I would highly recommend using the built in validation controls of asp.net. You can read more about them here. This has the added benefit of providing validation on the client, providing faster UI responses and easing the load off the servers.
For resetting all the textboxes. I would recommend creating a new class for this purpose, then calling upon that class when needed instead of messing with the master page:
public class UIControlsHelper
{
public static void ClearTextboxes(Page page)
{
GetAllControls(page)
.Where(x => typeof(TextBox).IsAssignableFrom(x.GetType())
.ToList()
.ForEach(x => (TextBox)x.Text = string.Empty);
}
IEnumerable<Control> GetAllControls(Page page)
// Same as above, but with the page parameter replaced.
}
}
And in any of your pages:
UIControlsHelper.ClearTextboxes(this);
To access the controls in your child page do the following steps:
1-declare a variable of the type you want to access. For example if you want to access a Label in your child page use:
Label lbl_child=this.ContentPlaceHolder1.findcontrol("your label id in child page") as Label;
Now you have your label and you are free to make changes on it. Every change on this control will be reflected on the child control.
ContentPlaceHolder1 is your contentplace holder id so change it with your content id.
public void ClearTextboxes(Page page) {
GetAllControls(page)
.Where(x => typeof(TextBox).IsAssignableFrom(x.GetType()))
.ToList()
.ForEach(x => ((TextBox)x).Enabled=false);
}
I have a gridview which contains controls like checkbox dropdownlist textbox etc.. These controls are in TemplateField and some are in updatepanel in gridview. There is a EditTemplateField which has some controls and a button. When grid is in edit mode, Now I have to find all controls in this EditTemplateField in button click event. I know that this can be done using foreach loop but don't know how?
You can use the grid EditIndex to find the row that contains the controls that are in edit mode. From there you can get the control using the control ID.
TextBox txtItem = (TextBox)Grid1.Rows[Grid1.EditIndex].FindControl("txtItem");
To find all controls then try this:
foreach(Control c in Grid1.Rows[Grid1.EditIndex].Controls)
{
// do stuff in here.
}
If you have container controls in the row, and need to find things inside of them, then you need do do something to recurse down into their controls.
I don't understand though why you would need to loop though the controls, usually the controls in the edit template will be fixed, and you know what they are so accessing them directly with Findcontrol is the way to go.
If you mean in code you can do it like this:
foreach ( GridViewRow row in MyGridView.Rows )
{
TextBox myTextBox = (TextBox)row.FindControl("myTextBox");
}
Here's a nifty utility method. It's recursive so it will find nested stuff e.g.
var listOfControls = Utility.FindControlsOfType<TextBox>(yourGridRow);
If you don't know the exact type of the nested controls, use FindControlsOfType<WebControl> instead.
public static class Utility
{
public static List<T> FindControlsOfType<T>(Control ctlRoot)
{
List<T> controlsFound = new List<T>();
if (typeof(T).IsInstanceOfType(ctlRoot))
controlsFound.Add((T)(object)ctlRoot);
foreach (Control ctlTemp in ctlRoot.Controls)
{
controlsFound.AddRange(FindControlsOfType<T>(ctlTemp));
}
return controlsFound;
}
}
Take the generated html using Firebug and using http://jsfiddle.net you can include jquery and play with the html.
Add the event handler to the edit button like
For example
function edit(this)
{
var textboxID = $(this).parent().find("[id$='textBoxId']");
}
I have a form MainForm which is a Windows Forms form that contains many child controls. I want to call one function on MainForm that notifies all of its children. Does the Windows Forms form provide a means to do this? I played with update, refresh and invalidate with no success.
foreach (Control ctrl in this.Controls)
{
// call whatever you want on ctrl
}
If you want access to all controls on the form, and also all the controls on each control on the form (and so on, recursively), use a function like this:
public void DoSomething(Control.ControlCollection controls)
{
foreach (Control ctrl in controls)
{
// do something to ctrl
MessageBox.Show(ctrl.Name);
// recurse through all child controls
DoSomething(ctrl.Controls);
}
}
... which you call by initially passing in the form's Controls collection, like this:
DoSomething(this.Controls);
The answer from MusiGenesis is elegant, (typical in a good way), nice and clean.
But just to offer an alternative using lambda expressions and an 'Action' for a different type of recursion:
Action<Control> traverse = null;
//in a function:
traverse = (ctrl) =>
{
ctrl.Enabled = false; //or whatever action you're performing
traverse = (ctrl2) => ctrl.Controls.GetEnumerator();
};
//kick off the recursion:
traverse(rootControl);
No, there isn't. You must roll out your own.
On a side note - WPF has "routed events" which is exactly this and more.
You are going to need a recursive method to do this (as below), because controls can have children.
void NotifyChildren( control parent )
{
if ( parent == null ) return;
parent.notify();
foreach( control child in parent.children )
{
NotifyChildren( child );
}
}