title: Best practices when applying conditional formatting in data bound controls?
body: <p>I have a Repeater contol bound to a custom object (an EntitySpaces query) and have noticed there are a couple of ways to conditionally format the values being displayed. </p>
  
  <p>1) From my aspx I can call a method in my code-behind and pass through the bound value and use that to drive any conditional logic:</p>
  
  <pre><code>        &lt;a class="star" href="&lt;%#MakePackageSelectionUrl((int)DataBinder.Eval(Container.DataItem, "PackageId"))%&gt;"&gt;
  
  and then in the code-dehind:
  
      protected string MakePackageSelectionUrl(int packageId)
      {
                return string.Format("/Packages/NoAjax/ToggleStar.aspx?p={0}&amp;amp;s={1}&amp;amp;st={2}", packageId, _streamId, (int)_phase);
      }
  </code></pre>
  
  <p>2) I can hook into the ItemDataBound event, retrieve e.Item.DataItem as a DataRowView and then go crazy:</p>
  
  <pre><code>    protected void PackageList_ItemDataBound(Object sender, RepeaterItemEventArgs e)
      {
          if (e.Item.ItemType != ListItemType.Item &amp;&amp; e.Item.ItemType != ListItemType.AlternatingItem) { return; }
  
          DataRowView details = (DataRowView)e.Item.DataItem;
  
          EncodePackageName(e, details);
          EncodeStatusName(e);
          DisplayStarImage(e, details);
      }
  
      private static void EncodePackageName(RepeaterItemEventArgs e, DataRowView dr)
      {
          HtmlAnchor p = (HtmlAnchor)e.Item.FindControl("packageLink");
          if (p != null)
          {
              p.HRef = string.Format("/Packages/View.aspx?p={0}", dr["packageId"]);
              p.InnerHtml = HttpUtility.HtmlEncode((string)dr["packageName"]);
          }
      }
  </code></pre>
  
  <p>I've also noticed that using e.Item.FindControl() in the code-behind requires runat="server" on the control in the aspx which has a nasty habit of encoding ids and generally messing up the HTML.</p>
  
  <p>I'm keen to hear from anyone that has come up with a nice approach for dealing with these sorts of issues.</p>