title: Better, stronger typed way of getting at controls in asp.net?
body: <p>I have the following function:</p>
  
  <pre><code>public void rpSearchResults_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
  {
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
      {
          System.Data.Common.DbDataRecord rd = (System.Data.Common.DbDataRecord)e.Item.DataItem;
  
          Literal litCompanyName = (Literal)e.Item.FindControl("litCompanyName");
          Literal litCompanyLocation = (Literal)e.Item.FindControl("litCompanyLocation");
          Literal litCompanyActivity = (Literal)e.Item.FindControl("litCompanyActivity");
          HiddenField hdnUserID = (HiddenField)e.Item.FindControl("hdnUserID");
          HyperLink lnkEmail = (HyperLink)e.Item.FindControl("lnkEmail");
          HyperLink lnkMicrosite = (HyperLink)e.Item.FindControl("lnkMicrosite");
          Literal litRecommends = (Literal)e.Item.FindControl("litRecommends");
  
          litCompanyName.Text = rd["CompanyName"].ToString();
  
          // Construct location info
          litCompanyLocation.Text = "";
          string[] addressParts = {"City","Region","Postcode"};
          bool prior = false;
          foreach (String part in addressParts){
              if (prior) litCompanyLocation.Text = litCompanyLocation.Text + ", ";
  
              string addressBit = rd[part].ToString();
              if (addressBit == null || addressBit.Trim() == "") prior = false;
              else
              {
                  litCompanyLocation.Text = litCompanyLocation.Text + rd[part].ToString();
                  prior = true;
              }
          }
  
          // ... and so on, mapping stuff from the database to fields.
      }
  }
  </code></pre>
  
  <p>It is fired by an event on my Repeater, and is used to populate a list of search results. All the various controls being found are present in the Item Template for the repeater.</p>
  
  <p>This all works, but I hate having to do it like this. I hate having to search for a control by string, and I hate having to do casts to get the controls into a state I can manipulate. If anything goes wrong - for instance, if I make a typo - the error is only picked up at runtime, and it can often be quite difficult to track down, as these functions seem to fail silently, rather than creating a big noticeable error page.</p>
  
  <p>This code is written now, and it works. But in the future I'd like to write it in a different way that is strongly typed, so that any errors will make themselves known at compile time. Is there any way to do this?</p>