10

Using aspnet 3.5, c# - Is there a way to insert Html into a gridview row?

Lill Lansey
  • 4,775
  • 13
  • 55
  • 77
  • duplicate: http://stackoverflow.com/questions/431840/how-to-render-decoded-html-in-a-i-e-a-br-in-gridview-cell . Aaron Daniels' answer of setting the HtmlDecode property of a BoundColumn worked well for me. – Despertar Jun 10 '12 at 03:33

3 Answers3

13

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
  • 2,045
  • 1
  • 18
  • 21
  • 1
    Thanks! This worked. So did Steven's answer. Both are easy solutions. – Lill Lansey Oct 05 '09 at 20:01
  • 1
    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=@"Make this bold"; – Lill Lansey Oct 05 '09 at 20:07
3

Simply modify the Text property of a cell.

Steven
  • 13,501
  • 27
  • 102
  • 146
1

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.

Ryan Alford
  • 7,514
  • 6
  • 42
  • 56
  • Had tried this: it just prints out the html as text, is : Make this bold> with the view source using the asci chars for < and > and / – Lill Lansey Oct 05 '09 at 19:54
  • This is not a good approach. And also a Label wraps the text property values between a tag. – Raúl Roa Oct 05 '09 at 20:15
  • @Lansey , I tried it right after I posted and it worked for me. I used "hey hey" and the first hey was bolded and the second was not. – Ryan Alford Oct 05 '09 at 21:04