title: How do I add a event to an ASP.NET control when the page loads?
body: <p>ASP.Net web forms, .NET 2.0</p>
  
  <p>I have a dilemma.  I have an aspx page that has a custom control:</p>
  
  <pre><code>&lt;def:CustomControl ID="customID" runat="server" /&gt;
  </code></pre>
  
  <p>The custom control can be added multiple times to the ASPX page if certain criteria is met:</p>
  
  <pre><code>&lt;asp:Repeater runat="server" ID="options" OnItemDataBound="options_OnItemDataBound"&gt;
      &lt;HeaderTemplate&gt;
          &lt;table border="0" cellpadding="0" cellspacing="0" width="100%"&gt;
              &lt;tr&gt;
      &lt;/HeaderTemplate&gt;
      &lt;ItemTemplate&gt;
                  &lt;td&gt;
                      &lt;span&gt;
                          &lt;asp:Label runat="server" ID="optionName"&gt;
                          &lt;/asp:Label&gt;
                          &lt;asp:DropDownList runat="server" ID="optionValues" CssClass="PartOption" &gt;
                          &lt;/asp:DropDownList&gt;
                      &lt;/span&gt;
                  &lt;/td&gt;
      &lt;/ItemTemplate&gt;
      &lt;FooterTemplate&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
      &lt;/FooterTemplate&gt;
  &lt;/asp:Repeater&gt;
  </code></pre>
  
  <p>In the code behind of the aspx page, I wanted to append a event (OnSelectedItemChange) to the dropdown control that isn't actually on the ASPX page (yet).  However, when I try to do something like the following in the code behind:</p>
  
  <pre><code>foreach(Control eachControl in customID.Controls) {
      if (eachControl.HasControls) {
          Control DdControl = eachControl.FindControl("optionValues");  //always null
  
          if (DdControl != null &amp;&amp; DdControl is typeOf(DropDownList)) {
              DdControl = DdControl as DropDownlist;  //I might have the syntax wrong
                                                      //here, but you get the point.
  
               //add event to control.
          }
      }
  }
  </code></pre>
  
  <p>But All that eachControl has is a repeater, and it shows its childcontrol count as 0.  So when it get to the FindControl method, it always returns null.</p>
  
  <p><strong>QUESTION</strong>
  So what I am trying to do is add an event (OnSelectedItemChange) to the dropdownlist control inside the repeater (which is a custom control that gets added to the page if criteria is met).  When a selection is made in the drop down, I want it to trigger the Event method.  </p>
  
  <p>How can I either:</p>
  
  <p>A) Find a way to add the event if the dropdownlist control exists on the page?</p>
  
  <p>OR</p>
  
  <p>B) A way to be able to call a method in the code behind and pass the selections from all the drop down lists (since the custom control could be added more than once creating multiple drop downs?</p>
  
  <p><strong>NOTE</strong> I don't have the code in front of me, so if I have something syntactically incorrect, please overlook.  I also know that ID's are unique, so this is also assumed to be a problem with my logic. When it renders these, the ID's might be mangled by the asp name convention which I would assume I have to find a way around.</p>