2

I'm using ascx and I need to iterate through all of the controls and selects each that have cssClass attribute set to 'required'.

I have the following code:

foreach (Control masterControl in Page.Controls)
        {
            if (masterControl is MasterPage)
            {
                foreach (Control formControl in masterControl.Controls)
                {
                    if (formControl is System.Web.UI.HtmlControls.HtmlForm)
                    {
                        foreach (Control contentControl in formControl.Controls)
                        {
                            if (contentControl is ContentPlaceHolder)
                            {
                                foreach (Control childControl in contentControl.Controls)
                                {

                                }
                            }
                        }
                    }
                }
            }
        }

however.. i cannot access childControl.CssClass. How do I access it?

Thanks in advance!

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
Johan
  • 89
  • 3
  • 10

3 Answers3

4

CssClass property is a member of the WebControl class.

You have to check if the control is a webcontrol, or, if it's only a control, you can get the attribute "class" in the attributes collection.

for example, you can do :

List<WebControl> wcs = new List<WebControl>();
GetControlList<WebControl>(Page.Controls, wcs)
foreach (WebControl childControl in wcs)
{
     if(childControl.CssClass == "required") {
          // process the control
     }
}

You also have to iterate recursively. Code found here : Using C# to recursively get a collection of controls from a controlcollection :

private void GetControlList<T>(ControlCollection controlCollection, List<T> resultCollection)
where T : Control
{
    foreach (Control control in controlCollection)
    {
        //if (control.GetType() == typeof(T))
        if (control is T) // This is cleaner
            resultCollection.Add((T)control);

        if (control.HasControls())
            GetControlList(control.Controls, resultCollection);
    }
}
Community
  • 1
  • 1
Steve B
  • 36,818
  • 21
  • 101
  • 174
1

The Control class doesn't have that CssClass property, the WebControl does. So try to cast your childControl to WebControl. If that worked, then you can access the CssClass property.

WebControl webCtrl = childControl as WebControl;
if (webCtrl != null)
{
   webCtrl.CssClass = "test";
}
Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
  • Thanks heaps Hans. One thing arises now. I cannot access the Value property of the web controls? Why is that? – Johan Jun 21 '11 at 09:24
  • @Johan: same problem. "WebControl" doesn't have a Value property so you need to cast it further to the "real" type - which will only work if it really is that type. – Hans Kesting Jun 21 '11 at 09:31
0

Regarding your comment on the answer above, you need to first check that it is a WebControl and then cast it to a WebControl

var webControl = childControl as WebControl;

if(webControl != null)
{
   if(webControl.CssClass == 'required')
   // Do your stuff
}
Simon
  • 2,810
  • 2
  • 18
  • 23
  • 2
    CodeAnalysis would complain that you are casting twice ("is" and "as"). Better (as in faster) would be to use just "as" and then check for null. – Hans Kesting Jun 21 '11 at 09:34
  • 1
    Did not know that! Thanks for the tip. Not sure it deserved a -1 though as we are talking nanoseconds, if that. And my code would work. – Simon Jun 21 '11 at 14:07