Using aspnet 3.5, c# - Is there a way to insert Html into a gridview row?
3 Answers
Yes. Use the TemplateField, and then type your html directly into the markup. If the html is suppose to be dynamically created I would use a Literal instead of a Label.
<asp:GridView id="GridView1" runat="server">
<Columns>
<asp:TemplateField headertext="Column1">
<ItemTemplate>
<br />
<h1>
<%# Eval ("DataColumnName") %>
</h1>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Column2">
<ItemTemplate>
<asp:Literal id="Literal1" runat="server" text='<%# Eval ("DataColumnName2") %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
2 Comments
Lill Lansey
Thanks! This worked. So did Steven's answer. Both are easy solutions.
Lill Lansey
Am going with this solution. Not hardcoding column position into my code and also consistent with the other ways I am dynamically populating my GridView: Literal theLit = (Literal)r.FindControl("Literal1"); theLit.Text=@"<b>Make this bold</b>";
I haven't tested this, but you should be able to add a Label control to the GridView cell. Then write your HTML to the Label's Text property. The Label should render the HTML.
3 Comments
Lill Lansey
Had tried this: it just prints out the html as text, is : <b>Make this bold</> with the view source using the asci chars for < and > and /
Raúl Roa
This is not a good approach. And also a Label wraps the text property values between a <span> tag.
Ryan Alford
@Lansey , I tried it right after I posted and it worked for me. I used "<b>hey</b> hey" and the first hey was bolded and the second was not.