Reputation: 5150
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?
Upvotes: 0
Views: 3684
Reputation: 10095
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>
<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.
- From Client goes to
- IIS Web Server
- ISAPI Extension
- ISAPI Extension loads/execute/converts aspx to HTML
- sends back to IIS Web Server.
- IIS responds back to client
Upvotes: 1
Reputation: 460028
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.
Upvotes: 3
Reputation: 6944
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" );
}
Upvotes: 1
Reputation: 9537
public void GetUserControls(ControlCollection controls)
{
foreach (Control ctl in controls)
{
if (ctl is CheckBoxOrWhateverControlTypeYouWant)
{
/// Add attribute
}
if (ctl.Controls.Count > 0)
GetUserControls(ctl.Controls);
}
}
Upvotes: 0