0

I have 11 of these controls on my page, all are checkboxes. It is contained within a master page.

I can accomplish what I want like so:

generalInformation.InputAttributes.Add( "class", "SetupChecklist" );
generalInformation2.InputAttributes.Add( "class", "SetupChecklist" );
generalInformation3.InputAttributes.Add( "class", "SetupChecklist" );

Etc..

I am now trying to loop through these and do the same thing to save myself some code, but I am having a lot of trouble getting this to work properly, well I can't get it to work at all.

Can anyone give me a good way to loop through these 11 checkbox controls and add the css class SetupChecklist?

I tried this and it isn't adding the class for some reason.

protected void InitializeCheckboxes ()
    {
        //generalInformation.InputAttributes.Add( "class", "SetupChecklist" );
        var allCheckBoxes = Page.Controls.OfType<CheckBox>();
        foreach ( var c in allCheckBoxes )
        {
            c.InputAttributes.Add( "class", "SetupChecklist" );
        } 
    }

I go call InitializeCheckboxes(); in the Page_Load method. It does work when I just use generalInformation.InputAttribues.Add etc.. But not when I loop through them. Any suggestions?

James Wilson
  • 5,074
  • 16
  • 63
  • 122

4 Answers4

3

The best would be to place them in a Panel(rendered as a div) or other container control. Then you can get the references with LINQ's OfType:

// assuming all checkboxes are in a panel named "SetupContainer"
var allCheckBoxes = SetupContainer.Controls.OfType<CheckBox>();
foreach(var chb in allCheckBoxes)
    chb.InputAttributes.Add( "class", "SetupChecklist" );

Of course you can also use it to find all CheckBoxes on the whole Page, but that might be prone to errors.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • @JamesWilson: Actually that works only for the given container control and does not look recursively down into sub-containers(like GridView or FormView). If you really need to look down(what is not recommended since it's more error-prone and might be inefficient): http://stackoverflow.com/a/253962/284240 – Tim Schmelter Apr 18 '12 at 16:06
  • Alright, I added runat="server" to my content div, and then changes Page.Controls to checklistContent.Controls. And it seems to be working just fine, thank you very much! – James Wilson Apr 18 '12 at 16:09
1

not tested but may help you..

foreach(Control oControl in Page.Controls)
{
  if(oControl is CheckBox && ((CheckBox)oControl).ID.StartsWith("generalInformation") )
   ((CheckBox)oControl).InputAttributes.Add( "class", "SetupChecklist" );
}
hkutluay
  • 6,794
  • 2
  • 33
  • 53
1

Your Checkbox will render like below due to runat = "server".

<span class="SetupChecklist" class="SetupChecklist" name="generalInformation">
     <input id="generalInformation" type="checkbox" name="generalInformation" />
</span>

JQuery

<script type="text/javascript" language="javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" language="javascript" src="Scripts/jquery-1.4.1.js"></script>
<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        $(this).find("input[type='checkbox']").addClass('GuestClass');
    });
</script>

This will save your time for below steps.

  1. From Client goes to
  2. IIS Web Server
  3. ISAPI Extension
  4. ISAPI Extension loads/execute/converts aspx to HTML
  5. sends back to IIS Web Server.
  6. IIS responds back to client
Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • That is an alternate solution I could have used. I did need to figure out how to loop through them anyway, as they will check a database to see if they should be checked or not. But this solution would work for adding the class. – James Wilson Apr 18 '12 at 16:17
0
public void GetUserControls(ControlCollection controls)
{
    foreach (Control ctl in controls)
    {
        if (ctl is CheckBoxOrWhateverControlTypeYouWant)
        {
             /// Add attribute
        }

        if (ctl.Controls.Count > 0)
            GetUserControls(ctl.Controls);
    }
}
KingOfHypocrites
  • 9,316
  • 9
  • 47
  • 69