1

In my application i have 50 textboxes i want find all the textbox control using the code and i want to perform a color change in the textbox after doing certain validations. How can i acheive that? i used the following code but it doesnt work properly

foreach (Control cntrl in Page.Controls)
    {
        if (cntrl is TextBox)
        {
            //Do the operation
        }
    }

<%@ Page Language="C#" MasterPageFile="~/HomePageMaster.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" Title="Sample Page" %>

Mathew Paul
  • 647
  • 3
  • 16
  • 36
  • 2
    Looks right to me, with the caveat that this will not do a recursive search, so if you have a container control within your page, any textboxes in that will not be found. What do you mean by "doesnt work properly"? – Oded Dec 14 '10 at 10:37
  • I think Oded is right, you've most likely got them in container controls – TBohnen.jnr Dec 14 '10 at 10:39
  • Can go through the SO link http://stackoverflow.com/questions/4321458/enumerate-all-controls-in-the-form/4333243#4333243 – dhinesh Dec 14 '10 at 10:49
  • @Oded iam getting only the master control in Page.Controls i am not getting other controls in that – Mathew Paul Dec 14 '10 at 11:21
  • @Mathew Paul - Can you post (a simplified version) of your aspx? At least so we can see what the page looks like? – Oded Dec 14 '10 at 11:22
  • <%@ Page Language="C#" MasterPageFile="~/HomePageMaster.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" Title="Sample Page" %>
    – Mathew Paul Dec 14 '10 at 11:48

4 Answers4

3

I've recently started doing this the 'modern' LINQ way. First you need an extension method to grab all the controls of the type you're interested in:

//Recursively get all the formControls  
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<Checkbox>();

foreach(Checkbox chbx in formCtls){

//do what you gotta do ;) }

Regards,
5arx

immutabl
  • 6,857
  • 13
  • 45
  • 76
1
protected void setColor(Control Ctl)
{
    foreach (Control cntrl in Ctl.Controls)
    {
         if (cntrl.GetType().Name == "TextBox")
         {
              //Do Code
         }
         setColor(Control cntrl);
    }
}

You can then call this with setColor(Page)

TBohnen.jnr
  • 5,117
  • 1
  • 19
  • 26
  • @Madhur It is but it searchs within the each control it finds instead of the pages.controls collection – Matt Dec 14 '10 at 10:56
  • you may put that setColor(Control ctrl) call into else{} since you can't have a textbox in a textbox. – Pabuc Dec 14 '10 at 11:01
1

This one goes recursively, so it will run on all controls in the page. Note that if you want it to go over the textboxes in the databound controls too, you should probably call it in the OnPreRender.

protected void Page_Load(object sender, EventArgs e)
{
    ColorChange(this);
}

protected static void ColorChange(Control parent)
{
    foreach (Control child in parent.Controls)
    {
        if (child is TextBox)
            (child as TextBox).ForeColor = Color.Red;

        ColorChange(child);
    }
}
Oleg Grishko
  • 4,132
  • 2
  • 38
  • 52
0

You will probably have to recursively go through each container. This article has one method of doing it.

Lareau
  • 1,982
  • 1
  • 26
  • 47