【发布时间】: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