4

I have a asp.net page which is inherited from a master page .I want to clear all controls in this page .I tried using the bellow method .This is not working if a master page is there. Otherwise its working fine 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;
            }
        }
    }
} 
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • Did you mean that you want to clear the TextBoxes in both the Page and its MasterPage, or is it simply not clearing the TextBoxes in your Page when you use a MasterPage with it? – KBoek Jan 12 '11 at 09:31
  • This might've to do with where you call ClearControls() from ... – KBoek Jan 12 '11 at 09:31
  • Why isn't it working if a MasterPage is there? Whats the exception? What does the quickwatch show about Page.Controls? – citronas Jan 12 '11 at 09:32
  • it simply not clearing the TextBoxes in your Page when you use a MasterPage with it.I am not getting any errors –  Jan 12 '11 at 09:33

10 Answers10

6

try this:

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);
        }
    }
}

Ex.:

Control ctrl = this.FindControl("content");
FindAllTextBox(ctrl);
ogun
  • 1,314
  • 2
  • 16
  • 40
2

You should be able to 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;
    }
}
Knelis
  • 6,782
  • 2
  • 34
  • 54
1

This is probably because of your controls are inside of another container when you add a master 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 wouldn't do it this way though. Sometimes hardcoding is better. This would use a lot of resource when called on a page that contains lots of controls.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Pabuc
  • 5,528
  • 7
  • 37
  • 52
1

Don't hard 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 ;)

}
immutabl
  • 6,857
  • 13
  • 45
  • 76
0

Make a method on your .cs like this:

 //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;
           }
       }
   }
Fabooo
  • 168
  • 2
  • 7
0
        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);
            }

        }
Jack
  • 10,943
  • 13
  • 50
  • 65
  • Some explanation would have been nice, here, to help someone coming across this post later understand it, and why it might be an answer to the question posed. – Andrew Barber Sep 28 '12 at 07:40
0
    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);

            }
        }

    }

calling in aspx.cs file as

        getAllCtl(this.Form.Controls);

This is OK and tested work for all Master-child page and where ever multiple controls are contains in the page...

0

First, use operator as instead of is and cast:

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

Second, you can use ITextControl instead of TextBox.

And third, try next 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);
}

Usage:

foreach (var c in this.Page.Controls.GetChildControls<TextBox>())
{
    c.Text = String.Empty;
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
0

I had the same problem but I think I was making it too hard. I'm using an AJAX UpdatePanel control and I just referenced that instead of going all the way up to the MasterPage. This 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

Just keep the controls in 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 = " ";  
                    }  
                }  
Awais Qarni
  • 17,492
  • 24
  • 75
  • 137