using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace UT.WebParts.NewsWP
    public class NewsWP : WebPart
        protected override void CreateChildControls()


        protected override void Render(HtmlTextWriter writer)

            string newslist = "<ul id='news' runat='server'>";
            //news.InnerHtml = "";

            using (SPSite spSite = new SPSite(SPContext.Current.Web.Url))
                using (SPWeb web = spSite.OpenWeb())
                        if (web.Lists["News"] != null)
                            SPList List = web.Lists["News"];

                            foreach (SPListItem li in List.Items)
                                newslist += "<li><div class='date'>";
                                DateTime date = DateTime.Parse(li.GetFormattedValue("Date").Replace('/', '-'));
                                //DateTime date = new DateTime(Int32.Parse(d[2]), Int32.Parse(d[1]), Int32.Parse(d[0]));
                                newslist += date.ToString("MMM dd, yyyy");
                                newslist += "</div><div class='text-holder'><h4>";
                                newslist += "<a href='NewsDetail.aspx?UniqueID=" + li.ID.ToString() + "'>";
                                newslist += li.Title;
                                newslist += "</a></h4><p>";
                                newslist += li.GetFormattedValue("Description");
                                newslist += "<a href='NewsDetail.aspx?UniqueID=" + li.ID.ToString() + "'>Read More &gt;</a></p></div></li>";
                        newslist += "An error has occured in this web part.";
            newslist += "</ul>";
            //news.InnerHtml = newslist;

I am trying to put a custom webpart onto my page layout but it is being rendered twice for some reason. Above is the whole code for my NewWP.cs file. In the layout page I have the following. This code creates two webparts, one that is editable and one that is now. Am I forgetting to add something?

<%@ Register Tagprefix="NewsWP" Namespace="UT.WebParts.NewsWP" Assembly="UT, Version=, Culture=neutral, PublicKeyToken=3f63cab1c16c2fff" %>
<WebPartPages:WebPartZone id="g_0A8F3D3F6B3B43D4BB9FC39E55892F0C" AllowPersonalization="true" runat="server" title="Zone 3"><ZoneTemplate>
<NewsWP:NewsWP AllowEdit="true" ChromeType="None" AllowHide="true" runat="server" id="NewsWP"></NewsWP:NewsWP>
  • I have am having the same issue. No idea whats going on. When I debug into the code it actually runs the webpart code twice. Dec 3, 2011 at 0:03

2 Answers 2


First of all you're doing the number one mistake when building Web Parts (or ASP.NET controls for that matter). Never ever write code like this in the Render() method. Use the CreateChildControls() preferrably (IMHO) and if that not suits you use RenderContents(). If you're using/overriding the Render() you can break all kinds of things.

Then; never ever iterate over all list items using SPList.Items, it's just bad and will kill performance. Use SPQuery and the GetItems() method to retrieve only the items and columns you need.

Then; why are you creating new SPSite and SPWeb objects? They are already there in SPContext.Current.Site and SPContext.Current.Web.

Then; you're trying to write a ul tag with runat="server" - that won't work. It will only be an invalid attribute in the resulting HTML

Then; since your concatenating strings I suggest you're looking into the StringBuilder class. For each string operation you're doing you're just allocating new stuff on the heap

Then; if you're writing code for SP2010 use the SPWeb.Lists.TryGetList(), that will save you some exception handling

Just a few pointers on your way to building the perfect Web Part!

  • +1 Too bad i can't vote twice :) Great job helping someone forward!
    – Dribbel
    Aug 19, 2011 at 18:47
  • What part of this actually answers his question? I'm just curious. Aug 24, 2011 at 19:21

Try defining your web part in the element file when deploying your page layout. Web parts that you define in the markup of your page layout will get extracted and stored in the web part management during the deployment. This way your pages will have the web part from the web part management and also display the web part from the page layout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.