Rendering HTML Easily within an SPGridView

If you try to create an SPGridView object, and need to include HTML formatted data within that SPGridView, you will quickly notice after viewing it within SharePoint, that the HTML is rendered as straight HTML, and not a link or an image, but, as the HTML code itself.

An easy way around this is to set the HTMLEncode property to false. By default this property is set to true, which will encode your HTML so that it will show up as code, changing <‘s and >’s into &lt; and &gt;, and so on.

Here is an example of this in action.

#region Create Child Controls
protected override void CreateChildControls()
{
    try
    {
        base.CreateChildControls(); 

        psGrid = new SPGridView();
        psGrid.AutoGenerateColumns = false; 

        BoundField colTitle = new BoundField();
        colTitle.HeaderText = "Title";
        colTitle.HtmlEncode = false;
        colTitle.DataField = "Title";
        this.psGrid.Columns.Add(colTitle); 

        BoundField colDesc = new BoundField();
        colDesc.HeaderText = "Description";
        colDesc.DataField = "Description";
        this.psGrid.Columns.Add(colDesc); 

        this.Controls.Add(this.psGrid); 

    }
    catch (Exception ex)
    {
        Trace.Write(ex);
        Trace.WriteLine(HttpContext.Current.Request.Url.ToString());
    }
}
#endregion 

#region Render
protected override void Render(HtmlTextWriter writer)
{
    try
    {
        using (SPSite site = new SPSite(SPContext.Current.Site.ID))
        {
            using (SPWeb web = site.OpenWeb("/sites/"))
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Title");
                dt.Columns.Add("Description"); 

                foreach (SPWeb subweb in web.Webs)
                {
                    DataRow dr = dt.NewRow();
                    dr["Title"] = "<a href='" + subweb.Url + "'>" + subweb.Title + "</a>";
                    dr["Description"] = subweb.Description;
                    dt.Rows.Add(dr);
                }
                this.psGrid.DataSource = dt;
                this.psGrid.DataBind(); 

                base.Render(writer);
            }
        } 

    }
    catch (Exception ex)
    {
        Trace.Write(ex);
        Trace.WriteLine(HttpContext.Current.Request.Url.ToString());
    }
}
#endregion 

 

Advertisement

About Geoff Varosky
Geoff Varosky is a Senior Architect for Insight, based out of Watertown, MA. He has been architecting and developing web based applications his entire career, and has been working with SharePoint for the past 15 years. Geoff is an active member of the SharePoint community, Co-Founder and Co-Organizer of the Boston Area SharePoint Users Group, co-founder for the Boston Office 365 Users Group, co-organizer for SharePoint Saturday Boston and speaks regularly at SharePoint events and user groups.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: