title: How to loop through controls to get specific type of controls?
body: <p>I have a div like this:</p>
  
  <pre><code>&lt;div id="columns" runat="server"&gt;
         &lt;ul id="column1" class="column" &gt;
          &lt;!-- /////////////// --&gt;
          &lt;/ul&gt;
          &lt;ul id="column2" class="column" runat="server"&gt;
          &lt;/ul&gt;
          &lt;ul id="column3" class="column" runat="server"&gt;
          &lt;/ul&gt;
          &lt;ul id="column4" class="column" runat="server"&gt;
              &lt;asp:PlaceHolder ID="PlaceHolder1" runat="server"&gt;&lt;/asp:PlaceHolder&gt;
          &lt;/ul&gt;
      &lt;/div&gt;
  </code></pre>
  
  <hr>
  
  <p>In run time I create <strong>listitems(<code>HtmlGenericControl outer_li = new HtmlGenericControl("li");</code>)</strong> in the (<code>ul</code> which has attribute <code>runat ="server"</code>).</p>
  
  <p>Now what i wanna to do is:
  looping through <code>div id="columns"</code> to get each (<code>ul</code> which has attribute <code>runat ="server"</code>) then loop through each (ul) to get each list item to save contents .</p>
  
  <hr>
  
  <h1>EDIT: according to answers:</h1>
  
  <pre><code>foreach (Control c in columns.Controls.OfType&lt;HtmlGenericControl&gt;())
          {
              var ctrl = (HtmlGenericControl)c;
  
              if (ctrl.TagName == "ul" &amp;&amp; ctrl.ID != "column1")
              {
                  foreach (Control li in ctrl.Controls.OfType&lt;HtmlGenericControl&gt;())
                  {
  
                      var ctrl_li = (HtmlGenericControl)li;
                      if (ctrl_li.TagName == "li")
                      {
                          string id = ctrl_li.ID;
                      }
                  }
              }
          }
  </code></pre>
  
  <p>this doesn't work i can't get the 'li' at all although there are <code>li</code> on my page </p>
  
  <hr>
  
  <h1>Edit 2:</h1>
  
  <pre><code> protected void CreateBlockOfData(string widget_color, int column_par, string process_name, int block_type, int block_id)
          {
              HtmlGenericControl outer_li = new HtmlGenericControl("li");
              outer_li.Attributes.Add("class", widget_color);
              if (column_par == 1)
              {
                  column1.Controls.Add(outer_li);
              }
              else if (column_par == 2)
              {
                  //uppnl_2.ContentTemplateContainer.Controls.Add(outer_li);
                  column2.Controls.Add(outer_li);
              }
              else if (column_par == 3)
              {
                  column3.Controls.Add(outer_li);
              }
              else if (column_par == 4)
              {
                  column4.Controls.Add(outer_li);
              }
              ////////////////////////////////////////////////////////////
              HtmlGenericControl div_head = new HtmlGenericControl("div");
              div_head.Attributes.Add("class", "widget-head");
              outer_li.Controls.Add(div_head);
              ////////////////////////////////////////////////////////////
              HtmlGenericControl h3 = new HtmlGenericControl("h3");
              div_head.Controls.Add(h3);
              ///////////////////////////////////////////////////////////
              Label lbl_process_name = new Label();
              lbl_process_name.Text = process_name.TrimEnd();
              h3.Controls.Add(lbl_process_name);
              ///////////////////////////////////////////////////////////
              HtmlGenericControl div_content = new HtmlGenericControl("div");
              div_content.Attributes.Add("class", "widget-content");
              outer_li.Controls.Add(div_content);
              ////////////////////////Data//////////////////////////////
              Control crl_data = FormTheData(block_type, block_id);
              PlaceHolder1.Controls.Add(crl_data);
              crl_data.DataBind();
              div_content.Controls.Add(crl_data);
          }
  </code></pre>