title: ASP.NET OnClick not firing in Repeater.ItemTemplate that has a custom control
body: <p>Title is a bit of a mouthful, but I'm a bit stuck on this issue. </p>
  
  <p>I have a search result page with has a custom control that has a <code>Repeater</code>. The <code>ItemTemplate</code> in the repeater is a <code>PlaceHolder</code> so I can construct the Item in a particular format; more specifically, I have a string of 'diseases' that come in the form of <code>Disease1|disease2|disease3</code> and need to be given links for each disease.</p>
  
  <p>Now for some code:</p>
  
  <p>The following is the <strong>SearchResultControl.ascx</strong></p>
  
  <pre><code>&lt;asp:Panel ID="SearchResultPanel" runat="server" ScrollBars="Auto"&gt;
      &lt;asp:Repeater ID="Repeater1" runat="server" 
          onitemcreated="Repeater1_ItemCreated" 
          onitemcommand="Repeater1_ItemCommand"&gt;
          &lt;ItemTemplate&gt;
              &lt;asp:PlaceHolder ID="ItemTemplatePlaceHolder" runat="server"&gt;
              &lt;/asp:PlaceHolder&gt;
          &lt;/ItemTemplate&gt;
          &lt;SeparatorTemplate&gt;
          &lt;tr&gt;
          &lt;td colspan="6"&gt;&lt;hr /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;/SeparatorTemplate&gt;
      &lt;/asp:Repeater&gt;
  &lt;/asp:Panel&gt;
  </code></pre>
  
  <p>The code behind: <strong>SearchResultControl.ascx.cs</strong></p>
  
  <pre><code>protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
  {
      if (e.Item.DataItem != null)
      {
          PlaceHolder placeHolder = e.Item.FindControl("ItemTemplatePlaceHolder") as PlaceHolder;
          Control searchResultItem = Page.LoadControl("SearchResultItem.ascx");
  
          DataRow row = (e.Item.DataItem as DataRowView).Row;
  
          if (row != null)
          {
              string diseaseState = row["DiseaseStates"] as string;
              searchResultItem.GetType().GetProperty("DiseaseStates").SetValue(searchResultItem, diseaseState, null);
  
              placeHolder.Controls.Add(searchResultItem);
          }
      }
  }
  </code></pre>
  
  <p>(Full disclosure, I got this idea from <a href="https://stackoverflow.com/questions/128083/dynamically-adding-controls-in-asp-net-repeater">this question</a>)</p>
  
  <p>The SetValue calls the DiseaseStates property in <strong>SearchResultItem</strong> which in turn calls the following method to build the links, set the text, and the events:</p>
  
  <pre><code>private void BuildDiseaseStateLabels(string diseaseStates)
  {
      PlaceHolder placeHolder = FindControl("DiseaseStatePlaceHolder") as PlaceHolder;
      string[] diseaseStateSplit = diseaseStates.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  
      int count = diseaseStateSplit.Length;
      foreach (string diseaseState in diseaseStateSplit)
      {
          LinkButton diseaseStateLink = new LinkButton();
          diseaseStateLink.Text = diseaseState;
          //diseaseStateLink.Click += new EventHandler(OnDiseaseStateLinkClick);
          diseaseStateLink.CommandArgument = "&lt;%# Eval(\"PatientID\")+ \";\" + Eval(\"PatientStatus\")+ \";\" + Eval(\"Age\")+ \";\" + Eval(\"DiseaseStates\")%&gt;";
          diseaseStateLink.CommandName = "OnDiseaseStateLinkClick";
          //diseaseStateLink.Command += new CommandEventHandler(OnDiseaseStateLinkClick);
  
          placeHolder.Controls.Add(diseaseStateLink);
  
          if (count != 0)
          {
              Label splitLabel = new Label();
              splitLabel.Text = "|";
              placeHolder.Controls.Add(splitLabel);
          }
      }
  }
  </code></pre>
  
  <p>This is the layout for the <strong>SearchResultItem</strong></p>
  
  <pre><code>&lt;div id="SearchResultItemDiv" class="MainSearchResultItem"&gt;
      &lt;asp:PlaceHolder ID="DiseaseStatePlaceHolder" runat="server"&gt;
      &lt;/asp:PlaceHolder&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Initially I tried setting the <code>Click</code> event, but that doesn't work at all. I then set the <code>CommandArgument</code> and <code>CommandName</code> but that didn't seem to do the trick. I figured the <code>Command</code> event might need to be set, but again, no luck. I should note that when a link is clicked the <code>Repeater1_ItemCreated</code> in <strong>SearchResultControl.ascx.cs</strong> is called. But since there is no data in <code>e.Item.DataItem</code> is null and I lose the results.</p>
  
  <p>In one of the <a href="https://stackoverflow.com/questions/1940206/imagebutton-in-asp-net-repeater-does-not-fire-onclick-eventhandler">questions</a> regarding the same issue, it was suggested that the <code>OnItemCommand</code> be added, but even that doesn't get called. </p>
  
  <p>I also read <a href="http://scottonwriting.net/sowBlog/archive/0000/00/00/162928.aspx" rel="nofollow noreferrer">A Stumper of an ASP.NET Question</a> and <a href="http://scottonwriting.net/sowBlog/archive/0000/00/00/162929.aspx" rel="nofollow noreferrer">A Stumper of an ASP.NET Question: SOLVED!</a>, to no avail. </p>
  
  <p>What could I possibly be doing wrong? All of the correct event hookups seem there, I'm checking for <code>IsPostBack</code> and not doing <code>DataBind()</code> again. <em>blaaargharaggh</em></p>
  
  <p>Help is always greatly appreciate.</p>