Sometimes we need to set the style sheet of a particular control dynamically. This can easily be done by traversing down the Page Control Hirarchy.
private void Button1_Click(object sender, System.EventArgs e)
{
// I made 10 Labels controls dynamically and put them inside the Panel control
int counter = 0;
string style = ddlStyle.SelectedItem.Value;
// Go into the Page Control Collection
foreach(Control c in Page.Controls)
{
if(c.HasControls())
{
// Go inside the form element
foreach(Control c2 in c.Controls)
{
// go inside the Panel control
foreach(Control c3 in c2.Controls)
{
// Check if the control is label or not
if(c3.GetType() == new Label().GetType())
{
// Set the CSS class
Label l = (Label) c3;
// Set the Style
l.CssClass = style;
}
}
}
}
}