0

i created html generic controls as (ul) and (il) at runtime then iwanna to loop inside my page to get all ul and il crated in run time wrote this code to loop inside page controls

foreach (HtmlGenericControl item in Page.Controls)
{


}
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
ahmed naguib
  • 145
  • 14

1 Answers1

1

Use the OfType<T>() extension method to get only HtmlGenericControls.

foreach (HtmlGenericControl item in Page.Controls.OfType<HtmlGenericControl>())
{


}
jrummell
  • 42,637
  • 17
  • 112
  • 171
  • 1
    You may also need to [search recursively](http://stackoverflow.com/questions/4955769/better-way-to-find-control-in-asp-net). – jrummell Apr 06 '12 at 14:13
  • sorry but when i created ul and li at runtime and click button these il or ul disappear so page .controls there is no htmlgenericcontrol in it how i keep these htmlgenericcontrols in page when postback – ahmed naguib Apr 06 '12 at 22:04
  • You'll need to show us how you are adding controls. It's likely that you are only adding them if `IsPostBack` is false. I believe you need to add them to the page in `OnInit` regardless of `IsPostBack`. – jrummell Apr 06 '12 at 23:41