2014-01-31 155 views
4

我在谷歌搜索了很多。但是,我沒有得到任何線索。 感謝大家提前。我有一個html源代碼在我的數據表column.when它與我的gridview綁定,我需要顯示在我的gridview列html輸出。那可能是如何將html綁定到Gridview列?

電流輸出:

enter image description here

我的aspx代碼:

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>"; 
    } 

只是爲了舉例來說,在地址欄我已經給定的HTML標記爲「破發標籤「但是在輸出中它只是顯示爲一個字符串,結果並不如預期。

注意:我不想使用模板字段而不是BoundField。

+0

你不介意使用jQuery? – naveen

回答

4

使用嘗試 - 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

0

您可以如果您使用的是.NET 4.0+,你也可以使用WebUtility.HtmlDecode不需要額外的程序集引用,因爲它是在System.Net命名空間可使用HttpUtility.HtmlDecode

3

設置HtmlEncode爲false您bounfield財產

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

BoundField HTML Encode Property MSDN

+0

這是HTML輸出。沒有@Damien,它不會 – naveen

0

設計你的GridView這樣

<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>