How to bind html to a gridview column?

I searched a lot about it on google. But I had no idea. Thank you all in advance.

Question: I have a html source in my datatable column.when it binds to my gridview, I need to show this html output in my gridview column. Is this possible ?

current output:

enter image description here

My aspx code:

 protected void Page_Load(object sender, EventArgs e)
 {
    DataTable dtEmployees = new DataTable();
    dtEmployees.Columns.Add(new DataColumn("FirstName", typeof(System.String)));
    dtEmployees.Columns.Add(new DataColumn("LastName", typeof(System.String)));
    dtEmployees.Columns.Add(new DataColumn("HomePhone", typeof(System.String)));
    dtEmployees.Columns.Add(new DataColumn("CellPhone", typeof(System.String)));
    dtEmployees.Columns.Add(new DataColumn("Address", typeof(System.String)));

    DataRow drEmpDetail = dtEmployees.NewRow();
    drEmpDetail["FirstName"] = "Tony";
    drEmpDetail["LastName"] = "Greg";
    drEmpDetail["HomePhone"] = "000-000-0000";
    drEmpDetail["CellPhone"] = "000-000-0000";
    drEmpDetail["Address"] = "Lane 1 Suite # 2 <br>";
  }

Just, for example, in the "Address" column, I gave the html tag for the "break tag". But at the output, it simply displays as a string, the result does not match what was expected.

Note: I do not want to use the Template field instead of the BoundField.

+4
source share
4 answers

Try using - HttpUtility.HtmlDecode("Lane 1 Suite # 2 <br>")

,

<asp:TemplateField HeaderText="Address">
    <ItemTemplate>
        <%# HttpUtility.HtmlDecode(Eval("Address").ToString()) %>
    </ItemTemplate>
</asp:TemplateField> 

: http://msdn.microsoft.com/en-us/library/7c5fyk1k.aspx

+4

HtmlEncode false bounfield

<asp:BoundField HeaderText="Address" DataField="YourDataField" HtmlEncode="false" />

BoundField HTML Encode Property MSDN

+3

You can use HttpUtility.HtmlDecode

If you are using .NET 4.0+, you can also use WebUtility.HtmlDecode, which does not require an additional assembly reference since it is available in the System.Net namespace.

0
source

Create a grid this way

<asp:GridView runat="server" ID="gv1" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField HeaderText="FirstName"  DataField="FirstName" />
            <asp:BoundField HeaderText="LastName"  DataField="LastName" />
            <asp:BoundField HeaderText="HomePhone"  DataField="HomePhone" />
            <asp:BoundField HeaderText="CellPhone"  DataField="CellPhone" />
            <asp:BoundField HeaderText="Address"  DataField="Address" HtmlEncode="false" />
        </Columns>
    </asp:GridView>
0
source

Source: https://habr.com/ru/post/1524690/


All Articles