asp 复选框列表项未呈现正确的文本

asp checkboxlist item not rendering correct text

我使用 asp 复选框列表来获得该结果

但是使用 html 标记作为复选框列表的项目,asp 会将其解释为 html。它适用于简单的文本。这是我的结果。

这里是声明和绑定方法

<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();

我认为您需要 HtmlEncode RadioButtonList 中的值。

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

但是您是直接绑定数据表,您要么必须在数据表的源中进行绑定,要么循环所有行并对它们进行编码。

        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();

尝试 this.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();