2016-09-08 40 views
0

我用asp的CheckBoxList有這種結果 enter image description hereASP CheckBoxList的項目不正確渲染文本

但使用HTML標記爲CheckBoxList的項目,ASP是將其解釋爲HTML。它適用於簡單的文本。這是我的結果。

enter image description here

,這裏是申報和裝訂方法

<asp:CheckBoxList ID="chklstreponse" runat="server"> 
        </asp:CheckBoxList> 

DataTable dtreponse = gq.GetRandom_Responses(Convert.ToInt32(idquest.Value)); 
     chkList.DataSource = dtreponse; 
     chkList.DataTextField = "libelle"; 
     chkList.DataValueField = "id"; 
     chkList.DataBind(); 

回答

0

我認爲你需要HtmlEncode在單選按鈕列表中的值。

System.Net.WebUtility.HtmlEncode("<html>") 

但你是直接綁定一個DataTable,你要麼必須做它在數據表或循環的所有行的來源和對其進行編碼。

 foreach (DataRow row in dtreponse.Rows) 
     { 
      row["libelle"] = System.Net.WebUtility.HtmlEncode(row["libelle"].ToString()); 
     } 

     chkList.DataSource = dtreponse; 
     chkList.DataTextField = "libelle"; 
     chkList.DataValueField = "id"; 
     chkList.DataBind(); 
+0

您的第一個答案適用(編輯前)。需要在所有可數據行上應用System.Net.WebUtility.HtmlEncode,並將其作爲數據源 –

+0

確定嗎?我試過了,它不適合我... – VDWWD

+0

是的,它適合我。我v'e在你的回答中加入了一些代碼 –

0

試試這個.HtmlEncode確保文本在瀏覽器中正確顯示,並且不被瀏覽器解釋爲HTML。

<asp:CheckBoxList ID="chklstreponse" runat="server"> </asp:CheckBoxList> 

    DataTable dtreponse = gq.GetRandom_Responses(Convert.ToInt32(idquest.Value)); 
    chkList.DataSource = dtreponse; 
    chkList.DataTextField = Server.HtmlEncode("libelle"); 
    chkList.DataValueField = "id"; 
    chkList.DataBind(); 
+0

它不起作用。 –