Clear all controls on an ASP.NET page using the master page

I have an asp.net page that is inherited from the main page. I want to clear all the controls on this page. I tried using the following method. This does not work if there is a main page. Otherwise, his work is great for any ideas?

private void ClearControls() { foreach(Control c in Page.Controls) { foreach (Control ctrl in c.Controls) { if (ctrl is TextBox) { ((TextBox)ctrl).Text = string.Empty; } } } } 
+4
source share
10 answers

try the following:

 public void FindAllTextBox(Control ctrl) { if (ctrl != null) { foreach (Control c in ctrl.Controls) { if (c is TextBox) ((TextBox)c).Text = string.empty; FindAllTextBox(c); } } } 

Example:.

 Control ctrl = this.FindControl("content"); FindAllTextBox(ctrl); 
+6
source

This is probably because your controls are inside another container when adding the main page. Have you tried adding another foreach before if?

 private void ClearControls() { foreach(Control container in Page.Controls) { foreach (Control c in container.Controls) { foreach (Control ctrl in c.Controls) { if (ctrl is TextBox) { ((TextBox)ctrl).Text = string.Empty; } } } } } 

I would not do that. Hard coding is sometimes better. This will use a lot of resources when called on a page that contains many controls.

+1
source

Do not print the code:

 //Recursively get all the formControls underneath the current one, be it Page, UserControl or whatever. public static IEnumerable<Control> GetAllControls(this Control parent) { foreach (Control control in parent.Controls) { yield return control; foreach (Control descendant in control.GetAllControls()) { yield return descendant; } } } 

Then you can call it in your webform / control:

 var formCtls = this.GetAllControls().OfType<TextBox>(); foreach(TextBox txtbx in formCtls) { //do what you gotta do ;) } 
+1
source

You can do this with Page.Form.FindControl("ContentPlaceHolder1").Controls :

 foreach (Control item in Page.Form.FindControl("ContentPlaceHolder1").Controls) { if (item is TextBox) { ((TextBox)item).Text = string.Empty; } } 
+1
source

First use the as operator instead of is and cast :

 TextBox tb = ctrl as TextBox; if (tb != null) { tb.Text = String.Empty; } 

Secondly, you can use ITextControl instead of TextBox .

And thirdly, try the following extension method:

 public static IEnumerable<T> GetChildControls(this Control control) where T : Control { var children = (control.Controls != null) ? control.Controls.OfType<T>() : Enumerable.Empty<T>(); return children.SelectMany(c => GetChildControls(c)).Concat(children); } 

Using:

 foreach (var c in this.Page.Controls.GetChildControls<TextBox>()) { c.Text = String.Empty; } 
0
source

I had the same problem, but I think I'm doing it too hard. I use the AJAX UpdatePanel control and I just referenced this, instead of going to MasterPage. It worked for me.

  foreach (Control c in UpdatePanel1.Controls) { foreach (Control c1 in c.Controls) { if (c1 is TextBox) { TextBox txtBox = (TextBox)c1; txtBox.Text = "0"; } } } 
0
source

Just hold the controls in the panel and try the code below

 foreach (Control cntrl in pnl.Controls)//pnl is panel id { if (cntrl is TextBox) { TextBox txtBox = (TextBox)cntrl; txtBox.Text = " "; } } 
0
source

Make a method on your .cs as follows:

  //Where "this" is Page. ClearInput(this); private void ClearInput(Control parent) { foreach (Control c in parent.Controls) { if (c.Controls.Count > 0) ClearInput(c); else { if (c is TextBox) (c as TextBox).Text = ""; if (c is CheckBox) (c as CheckBox).Checked = false; if (c is DropDownList) (c as DropDownList).SelectedIndex = 1; } } } 
0
source
  private void EnableControls(Control control) { var textbox = control as TextBox; if (textbox != null) { textbox.Enabled = true; } var dropDownList = control as DropDownList; if (dropDownList != null) { dropDownList.Enabled = true; } var radioButton = control as RadioButton; if (radioButton != null) { radioButton.Enabled = true; } var checkBox = control as CheckBox; if (checkBox != null) { checkBox.Enabled = true; } foreach (Control childControl in control.Controls) { EnableControls(childControl); } } 
0
source
  public void getAllCtl(ControlCollection ctls) { foreach (Control c in ctls) { if (c is System.Web.UI.WebControls.TextBox) { //TextBox tt = c as TextBox; ////to do something by using textBox tt. ((TextBox)c).Text = string.Empty; } if (c is System.Web.UI.WebControls.CheckBox) { ((CheckBox)c).Checked = false; } if (c is System.Web.UI.WebControls.DropDownList) { ((DropDownList)c).SelectedIndex = -1; } if (c.HasControls()) { getAllCtl(c.Controls); } } } 

call in aspx.cs file as

  getAllCtl(this.Form.Controls); 

This is normal and the operation is checked for all Master-child pages and where the page contains several controls ...

0
source

Source: https://habr.com/ru/post/1335216/


All Articles