htmlasp.nettagsasprepeater

How to show HTML data as normal string in Table in Repeater using asp.net?


I want to show HTML data as normal string in Table Data Cell. Following is my code and screenshot of result.

ASPX

<asp:Repeater ID="rptMainTable" runat="server">
    <HeaderTemplate>
    </HeaderTemplate>
    <ItemTemplate>
        <td><%# Eval("FieldValue").ToString()%></td>
    </ItemTemplate>
    <FooterTemplate>
    </FooterTemplate>
</asp:Repeater>

CS

private void addListData()
{
    List<TableClass> listTableAction = new List<TableClass>();
    listTableAction.Add(new TableClass(){ FieldValue = "<b>TestBold</b>"});
    listTableAction.Add(new TableClass(){ FieldValue = "test"});
    rptMainTable.DataSource = listTableAction;
    rptMainTable.DataBind();
}

Result

enter image description here


Solution

  • Use HtmlEncode

    <asp:Repeater ID="rptMainTable" runat="server">
        <HeaderTemplate>
        </HeaderTemplate>
        <ItemTemplate>
            <td><%# HttpUtility.HtmlEncode(Eval("FieldValue").ToString()) %></td>
        </ItemTemplate>
        <FooterTemplate>
        </FooterTemplate>
    </asp:Repeater>
    

    That will display the string as <b>TestBold</b>