views:

22

answers:

1

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
A: 

Got it. Since my boundfields were encased in a DetailsView, I used the onLoad event of the DetailsView to call a function in the code-behind to decode the any html within the text of the Boundfields

''' <summary>
''' Decodes any HTML formatted tags in the Title and Description Textboxes of the Edit Window
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Protected Sub HTMLDecode(ByVal sender As Object, ByVal e As System.EventArgs)
    If Page.IsPostBack = False Then
        ''Grab the Title and Description text boxes from the RowCollection
        Dim dvrTest As DetailsViewRowCollection = DetailsView1.Rows
        Dim TitleTB As TextBox = dvrTest.Item(0).Cells(1).Controls(0)
        Dim DescriptionTB As TextBox = dvrTest.Item(1).Cells(1).Controls(0)
        ''Decode HTML tags that are in either text box
        DescriptionTB.Text = HttpUtility.HtmlDecode(DescriptionTB.Text)
        TitleTB.Text = HttpUtility.HtmlDecode(TitleTB.Text)
    End If
End Sub 'HTMLDecode

And calling it in the DetailsView using the onLoad event

<asp:DetailsView ID="DetailsView1" runat="server" Height="260px" Width="500px" AutoGenerateRows="False"
            DataKeyNames="TS_ID" DataSourceID="SqlDataSource2" EnableModelValidation="true"
            GridLines="Both" Font-Names="Arial" HorizontalAlign="Center" OnLoad="HTMLDecode" >

If there are any more straightforward alternatives, I'd be glad to hear them.

kingrichard2005