By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,679 Members | 2,602 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,679 IT Pros & Developers. It's quick & easy.

Trap DataGrid in a each control loop

P: n/a
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[i].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!?
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

"marc-andr?" wrote:
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[i].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!?

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.