Search code examples
asp.netcheckboxlist

asp checkboxlist item not rendering correct text


I use asp checkboxlist to have that result enter image description here

But using html tag as item of the checkboxlist , asp is interpreting it as html. It works for simple text. Here is my result.

enter image description here

and here is the declaration and binding method

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

Solution

  • I think you need to HtmlEncode the values in the RadioButtonList.

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

    But you are binding a datatable directly, you either must do it in the source of the DataTable or loop all rows and encode them.

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