Trap DataGrid in a each control loop

  • Thread starter Thread starter marc-andr?
  • Start date Start date
M

marc-andr?

Hi,

I have to loop on each control on a webforms...

I use this function :

public void LoopOnAllControls(ControlCollection coll)
{
foreach (System.Web.UI.Control ctrl in coll)
{
if(ctrl.HasControls())
{
LoopOnAllControls(ctrl.Controls);
}
else
{
SetTheStyleClass(ctrl);
}

}

}


public void SetTheStyleClass(Control ct)
{
string TypeCt;
TypeCt = ct.GetType().ToString();

switch (TypeCt)
{

case "System.Web.UI.HtmlControls.HtmlAnchor" :
{
(ct as HyperLink).CssClass = HyperLink;
break;
}

case "System.Web.UI.HtmlControls.HtmlButton" :
{
(ct as Button).CssClass = BoutonDeCommande;
break;
}

case "System.Web.UI.WebControls.Button" :
{
(ct as Button).CssClass = BoutonDeCommande;
break;
}

case "System.Web.UI.HtmlControls.HtmlInputText" :
{
(ct as TextBox).CssClass = BoiteDeTextte ;
break;
}

case "System.Web.UI.WebControls.TextBox" :
{
(ct as TextBox).CssClass = BoiteDeTextte ;
break;
}

case "System.Web.UI.WebControls.Label" :
{
(ct as Label).CssClass = TexteOrdinaire ;
break;
}

case "System.Web.UI.WebControls.ListBox" :
{
(ct as ListBox).CssClass = ListAndComboBox;
break;
}

case "System.Web.UI.WebControls.DropDownList" :
{
(ct as DropDownList).CssClass = ListAndComboBox;
break;
}

case "System.Web.UI.WebControls.HyperLink" :
{
(ct as HyperLink).CssClass = HyperLink;
break;
}




case
"System.Web.UI.WebControls.DataGrid" :
{
//They never enter here!!!!!
DataGrid dtg = ct as DataGrid;

for(int i=0;i<dtg.Items.Count;i++)
{
dtg.Items.CssClass= "texte";
}

break;
}

}
}

}


the " SetTheStyleClass" is use for setting my CssClass on the
control... anyway there's my problem

It's works well for any control... but not with the DataGrid. I don't
know why?? I can't trap the DataGrid control...

Anyone have an idea!?
 
Has the Datagrid been bound before doing this loop. Also you may want to
revise your code. Iterating through each control can be detrimental to
performance.
 
Back
Top