title: Formatting text in an ASP.Net Boundfield
body: <p>I have a webform with a a couple of boundfields in an edit window as follows:  </p>
  
  <pre><code>&lt;asp:BoundField DataField="TS_TITLE" HeaderText="Title" SortExpression="TS_TITLE" HeaderStyle-VerticalAlign="Top" HtmlEncode="True" &gt;
              &lt;ControlStyle Width="500px" /&gt;
          &lt;/asp:BoundField&gt;
          &lt;custom:BoundTextBoxField DataField="TS_DESCRIPTION" HeaderText="Desription" HeaderStyle-VerticalAlign="Top" SortExpression="TS_DESCRIPTION"
              TextMode="MultiLine" Rows="20" Columns="100" Wrap="True" HtmlEncode="True"  /&gt;
  </code></pre>
  
  <p>I'm using the Html Encode property of the BoundField to secure against cross-site scripting attacks.  What I would like to do is when a user reopens the edit window, I want the encoded html to be decoded and presented, html tags and all.  My problem is that when I try to decode the html in the code-behind, under the Page_Load function, it doesn't get set when the page is presented to the user, i.e. it has no effect.  Here is the snippet of code from the Page_Load:</p>
  
  <pre><code>Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
          Dim dvrTest As DetailsViewRowCollection = DetailsView1.Rows
  
          Dim DescriptionTB As TextBox = dvrTest.Item(1).Cells(1).Controls(0)
          DescriptionTB.Text = HttpUtility.HtmlDecode(DescriptionTB.Text)
      End Sub 'Page_Load
  </code></pre>
  
  <p>When debugging I can see the html decoded text as it should look, my guess is that there's an additional databind that occurs after the Page_Load exits that resets the BoundTextBoxField.  Just a note, I've tested this on both the BoundField and BoundTextBoxField and the effect is the same on both.</p>
  
  <p>I had a similar issue with a dropdown list I'm using in another part of my application, only there I was using the onLoad event to call a function to do data manipulation after the page was loaded and databound.  Unfortunately, Boundfield doesn't seem to have that event, the closest thing I've found is DataFormatString property, but that only seems to be useful when working with dates and currency.</p>
  
  <p>UPDATE:</p>
  
  <p>In case anyone was wondering, even with the HTMLEncode property set to false, I get the encoded text when the Edit Window is Reloaded.</p>
  
  <p>UPDATE 2:</p>
  
  <p>Tried overriding the OnDataBinding method, but that didn't seem to do anything.</p>
  
  <pre><code>Protected Overrides Sub OnDataBinding(ByVal e As System.EventArgs)
          Me.OnDataBinding(e)
          Dim dvrTest As DetailsViewRowCollection = DetailsView1.Rows
          Dim DescriptionTB As TextBox = dvrTest.Item(1).Cells(1).Controls(0)
          DescriptionTB.Text = HttpUtility.HtmlDecode(DescriptionTB.Text)
      End Sub
  </code></pre>