title: Use a FOR loop to return SelectedValue of DropDownList
body: <p>I've been spending a lot of time searching for a solution to what seems to be a simple thing.</p>
  
  <p>The following returns the "Object reference not set to an instance of an object." error. I put this code on a separate blank page for troubleshooting. Any ideas?</p>
  
  <p>ASP.Net:</p>
  
  <pre><code>&lt;div&gt;
      &lt;asp:DropDownList ID="ChoosePattern1" ClientIDMode="Static" runat="server" CssClass="dropformat"
          AutoPostBack="true" OnSelectedIndexChanged="getimage"&gt;
          &lt;asp:ListItem&gt;1&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;2&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;3&lt;/asp:ListItem&gt;
      &lt;/asp:DropDownList&gt;
      &lt;asp:DropDownList ID="ChoosePattern2" ClientIDMode="Static" runat="server" CssClass="dropformat"
          AutoPostBack="true" OnSelectedIndexChanged="getimage"&gt;
          &lt;asp:ListItem&gt;1&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;2&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;3&lt;/asp:ListItem&gt;
      &lt;/asp:DropDownList&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>C# Codebehind:</p>
  
  <pre><code>protected void getimage(object sender, EventArgs e)
  {
      for (int intCounter = 1; intCounter &lt;= 8; intCounter++)
      {
          string mypattern = "ChoosePattern" + Convert.ToString(intCounter);
  
          DropDownList ddl = Page.FindControl(mypattern) as DropDownList;
          Response.Write(ddl.SelectedValue);
      }
  }
  </code></pre>