title: Cannot loop through web form Control in c#
body: <p>I have a simple web form with some dropdown and textboxes. I am trying to get the value of the all the elements by looping through the control. (note I am assuming that I dont know the name/id of the elements and hence I am trying to loop through them to get the information and store it in a database) However the when I loop through control I get count at 5 even though there are 4 input elements and the control are represented by System.Web.UI.LiteralControl instead of System.Web.UI.WebControls.TextBox or System.Web.UI.WebControls.dropdown</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  
      &lt;script src="Datepicker/jquery-1.7.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script src="Datepicker/jquery-ui.js" type="text/javascript"&gt;&lt;/script&gt; 
      &lt;link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" /&gt;
  
       &lt;script&gt;
           $(function () {
               $("#targetDueDate").datepicker();
           });
    &lt;/script&gt;
      &lt;script&gt;
          $(function () {
              $("#targetdeliveryoffiles").datepicker();
          });
    &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
          &lt;div&gt;
              &lt;table&gt;
  
                  &lt;tr&gt;
                      &lt;td&gt;Tier:
  
                      &lt;/td&gt;
  
                      &lt;td&gt;
                          &lt;asp:DropDownList ID="tier" runat="server"&gt;
                              &lt;asp:ListItem&gt;Select Tier&lt;/asp:ListItem&gt;
                              &lt;asp:ListItem&gt;Tier1&lt;/asp:ListItem&gt;
                              &lt;asp:ListItem&gt;Tire2&lt;/asp:ListItem&gt;
                              &lt;asp:ListItem&gt;Tier3&lt;/asp:ListItem&gt;
                          &lt;/asp:DropDownList&gt;
  
                      &lt;/td&gt;
                  &lt;/tr&gt;
  
                  &lt;tr&gt;
                      &lt;td&gt;Author: 
  
                      &lt;/td&gt;
  
                      &lt;td&gt;
                          &lt;asp:DropDownList ID="author" runat="server"&gt;
                              &lt;asp:ListItem&gt;Select Author&lt;/asp:ListItem&gt;
                              &lt;asp:ListItem&gt;Author1&lt;/asp:ListItem&gt;
                              &lt;asp:ListItem&gt;Author2&lt;/asp:ListItem&gt;
                              &lt;asp:ListItem&gt;Author3&lt;/asp:ListItem&gt;
                          &lt;/asp:DropDownList&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
  
  
                        &lt;tr&gt;
                      &lt;td&gt;Quotation For:
  
                      &lt;/td&gt;
  
                      &lt;td&gt;
                          &lt;asp:TextBox ID="quotationFor" runat="server"&gt;&lt;/asp:TextBox&gt;
  
                      &lt;/td&gt;
                  &lt;/tr&gt;
  
                  &lt;tr&gt;
                      &lt;td&gt;Previous Est #:&lt;/td&gt;
                      &lt;td&gt;
                          &lt;asp:TextBox ID="previousEst" runat="server"&gt;&lt;/asp:TextBox&gt;
                          &lt;asp:RegularExpressionValidator ID="RegularExpressionValidator1"
                              ControlToValidate="previousEst"
                              ValidationExpression="\d+"
                              Display="Dynamic"
                              EnableClientScript="true"
                              ErrorMessage="Please enter numbers only"
                              runat="server" ValidationGroup="a" ForeColor="Red" /&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
  
  
              &lt;/table&gt;
  
              &lt;br /&gt;
  
              &lt;div style="text-align: center; width: 327px;"&gt;
  
                  &lt;asp:Button ID="submit" runat="server" Text="Submit" CssClass="btnsubmit" ValidationGroup="a" OnClick="submit_Click" /&gt;
  
              &lt;/div&gt;
  
  
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The onsubmit code</p>
  
  <pre><code>protected void submit_Click(object sender, EventArgs e)
      {
          foreach(Control c in controls)
           {
              if(c is System.Web.UI.WebControls.TextBox)
              {
                  /// Do Something
              }
          }
      }
  </code></pre>
  
  <p>What am i doing wrong ? How do I loop through the element to get their values ? (also the ID since I need to store both in the database)</p>