Custom properties for custom webpart in wss 3.0

Writing custom properties for your own web part isn’t that complex. Here is a simple example to do that.

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using System.Web.UI.WebControls.WebParts;

namespace SampleWebPart
{
    [DefaultProperty("DisplayText"),
      ToolboxData("<{0}:Sample runat=server></{0}:Sample>"),
       XmlRoot(Namespace = "SampleWebPart")]
    public class Sample : WebPart
    {
        private string displayText = “Hello World!”;
        [WebBrowsable(true), Personalizable(true)]
        public string DisplayText
        {
            get { return displayText; }
            set { displayText = value; }
        }
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            writer.Write(displayText);
        }
     }
}

Posted in SharePoint.

Leave a Reply