title: How to use HtmlEncode with TemplateFields, Data Binding, and a GridView
body: <p>I have a GridView bound to an ObjectDataSource.  I've got it supporting editing as well, which works just fine.  However, I'd like to safely HtmlEncode text that is displayed as we do allow special characters in certain fields.  This is a cinch to do with standard BoundFields, as I just set HtmlEncode to true.</p>
  
  <p>But in order to setup validation controls, one needs to use TemplateFields instead.  How do I easily add HtmlEncoding to output this way?  This is an ASP.NET 2.0 project, so I'm using the newer data binding shortcuts (e.g. <code>Eval</code> and <code>Bind</code>).</p>
  
  <p>What I'd like to do is something like the following:</p>
  
  <pre><code>&lt;asp:TemplateField HeaderText="Description"&gt;
      &lt;EditItemTemplate&gt;
          &lt;asp:TextBox ID="TextBoxDescription" runat="server"
                       Text='&lt;%# System.Web.HttpUtility.HtmlEncode(Bind("Description")) %&gt;'
                       ValidationGroup="EditItemGrid"
                       MaxLength="30" /&gt;
          &lt;asp:Validator ... /&gt;
      &lt;/EditItemTemplate&gt;
      &lt;ItemTemplate&gt;
          &lt;asp:Label ID="LabelDescription" runat="server"
                     Text='&lt;%# System.Web.HttpUtility.HtmlEncode(Eval("Description")) %&gt;' /&gt;
      &lt;/ItemTemplate&gt;
  &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p>However, when I try it this way, I get the following error:</p>
  
  <blockquote>
    <p>CS0103: The name 'Bind' does not exist
    in the current context</p>
  </blockquote>