Using aspnet 3.5, c# - Is there a way to insert Html into a gridview row?
Wow. Cant believe how simple that was. Was going crazy with this.
Lill Lansey
2009-10-05 19:52:11
A:
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.
Eclipsed4utoo
2009-10-05 19:48:31
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 /
Lill Lansey
2009-10-05 19:54:16
This is not a good approach. And also a Label wraps the text property values between a <span> tag.
Raúl Roa
2009-10-05 20:15:41
@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.
Eclipsed4utoo
2009-10-05 21:04:26
+3
A:
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>
Chris
2009-10-05 19:50:26
Thanks! This worked. So did Steven's answer. Both are easy solutions.
Lill Lansey
2009-10-05 20:01:32
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>";
Lill Lansey
2009-10-05 20:07:26