I have a webform with a a couple of boundfields in an edit window as follows:
<asp:BoundField DataField="TS_TITLE" HeaderText="Title" SortExpression="TS_TITLE" HeaderStyle-VerticalAlign="Top" HtmlEncode="True" >
<ControlStyle Width="500px" />
</asp:BoundField>
<custom:BoundTextBoxField DataField="TS_DESCRIPTION" HeaderText="Desription" HeaderStyle-VerticalAlign="Top" SortExpression="TS_DESCRIPTION"
TextMode="MultiLine" Rows="20" Columns="100" Wrap="True" HtmlEncode="True" />
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:
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
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.
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.
UPDATE:
In case anyone was wondering, even with the HTMLEncode property set to false, I get the encoded text when the Edit Window is Reloaded.
UPDATE 2:
Tried overriding the OnDataBinding method, but that didn't seem to do anything.
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