2010-08-09 72 views
1

我有機管局夫婦在編輯窗口綁定列的Web窗體如下:設置文本格式在ASP.Net的BoundField

<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編碼性能,以確保對互網站腳本攻擊。我想要做的是,當用戶重新打開編輯窗口,我想要解碼和呈現編碼的html,html標籤和所有。我的問題是,當我嘗試在代碼隱藏中解碼HTML時,在Page_Load函數下,當頁面呈現給用戶時,它不會被設置,即它不起作用。下面是代碼在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 

調試時,我可以看到HTML解碼文本,因爲它應該是,我的猜測是,有說在Page_Load退出該復位BoundTextBoxField後發生額外的數據綁定。請注意,我已經在BoundField和BoundTextBoxField上測試了這一點,兩者的效果都是相同的。

我有一個類似的問題,我在我的應用程序的另一部分中使用下拉列表,只有在那裏我使用onLoad事件來調用一個函數來加載頁面和數據綁定後執行數據操作。不幸的是,Boundfield似乎沒有這個事件,我發現的最接近的東西是DataFormatString屬性,但這隻在使用日期和貨幣時纔有用。

UPDATE:

爲了防止有人懷疑,甚至與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 

回答

0

明白了。由於我的綁定列是在一個DetailsView包裹,我用DetailsView控件的onload事件調用的代碼隱藏的功能的綁定列

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

文本中的任何HTML進行解碼,並要求它在DetailsView使用onLoad事件

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

如果有任何更直接的選擇,我會很高興聽到他們的聲音。