【问题标题】:Formatting text in an ASP.Net Boundfield在 ASP.Net Boundfield 中格式化文本
【发布时间】:2011-03-27 13:29:01
【问题描述】:

我在编辑窗口中有一个带有几个绑定字段的网络表单,如下所示:

<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"  />

我正在使用 BoundField 的 Html Encode 属性来防止跨站点脚本攻击。我想做的是当用户重新打开编辑窗口时,我希望编码的 html 被解码和呈现,html 标签等等。我的问题是,当我尝试在 Page_Load 函数下的代码隐藏中解码 html 时,当页面呈现给用户时它没有设置,即它没有效果。以下是 Page_Load 中的代码 sn-p:

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

在调试时,我可以看到 html 解码后的文本,我的猜测是在 Page_Load 退出后会发生一个额外的数据绑定,它会重置 BoundTextBoxField。请注意,我在 BoundField 和 BoundTextBoxField 上都测试过,效果是一样的。

我在应用程序的另一部分中使用的下拉列表也有类似的问题,只是在页面加载和数据绑定后,我使用 onLoad 事件调用函数来执行数据操作。不幸的是,Boundfield 似乎没有那个事件,我发现最接近的是 DataFormatString 属性,但这似乎只在处理日期和货币时才有用。

更新:

如果有人想知道,即使 HTMLEncode 属性设置为 false,我也会在重新加载编辑窗口时获得编码文本。

更新 2:

尝试重写 OnDataBinding 方法,但这似乎没有任何作用。

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

【问题讨论】:

    标签: asp.net web-applications webforms


    【解决方案1】:

    知道了。由于我的 boundfields 被封装在 DetailsView 中,我使用 DetailsView 的 onLoad 事件调用代码隐藏中的函数来解码 Boundfields 文本中的任何 html

    ''' <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
    

    并使用 onLoad 事件在 DetailsView 中调用它

    <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" >
    

    如果有任何更直接的替代方案,我会很高兴听到它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-21
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-01
      • 2014-01-13
      • 2019-09-10
      相关资源
      最近更新 更多