Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a html to a Gridview column?

I have searched about this a lot in google. But, i didn't get any clue. Thanks to you all in advance.

Question: i have a html source in my datatable column.when it binds with my gridview, i need to show that html output in my gridview column. Is that 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 have given html tag for the "break tag".But in the output it just displays like a string, the result is not as expected.

Note: I don't want to use the Template field instead of BoundField.

like image 796
Madhan Sekar Avatar asked Jan 31 '14 12:01

Madhan Sekar


2 Answers

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

The markup will be,

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

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

like image 104
Anuraj Avatar answered Oct 27 '22 02:10

Anuraj


Set HtmlEncode to false in your bounfield property

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

BoundField HTML Encode Property MSDN

like image 44
Zo Has Avatar answered Oct 27 '22 00:10

Zo Has