Monday, December 2, 2013

Sharepoint Webpart Tool editor panel code for parameters

For Normal webpart:
Step1: inherit IWebEditable

public partial class NMCWebPart : WebPart
{

Step 2: Declare variable that are supposed to appear in Tool part editor

 [Category("Custom Properties")]
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [WebDisplayName("Title1")]
        [WebDescription("Main title the Web Part.")]
        public string TextMainTitle
        {
            get;
            set;
        }
        [Category("Custom Properties")]
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [WebDisplayName("Title2")]
        [WebDescription("Sub title the Web Part.")]
        public string TextSubTitle
        {    get;
            set;
        }
Step 3 : use these variables in our coding:

 protected override void Render(HtmlTextWriter writer)
        {

            //base.Render(writer);
            writer.Write("<h4><b>" + Title1+ "</b></h4><br>");
            writer.Write("<h3>" + Title2+ "<br><br>");
            writer.Write("<table><tr><td>");
            lblName.RenderControl(writer);
            writer.Write("</td><td>");
*****************OR********************************

        public override void RenderControl(System.Web.UI.HtmlTextWriter writer)
        {
            if (Title1!= null)
            {
                lblMainTitle.Text = Title1;
  
            }
                if (Title2!= null)
                {
                     lblSubTitle.Text = Title2;
                }

_______________________________________
For VS2013 webparts, It will have ascx control, so all the Rendering part will be covered by ascx design.

for this in desing I have taken two labels, label1 and label 2 and we don't want to write any Render method.

[Category("Custom Properties")]
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [WebDisplayName("Main Title")]
        [WebDescription("Main title the Web Part.")]
        public string TextMainTitle
        {
            get
            {
                if (label1 != null)
                {
                    return label1 .Text;
                }
                else
                {
                    return string.Empty;
                }
            }

            set
            {
                if (label1 != null)
                {
                    label1 .Text = value;
                }
            }
        }
        [Category("Custom Properties")]
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [WebDisplayName("Sub Title")]
        [WebDescription("Sub title the Web Part.")]
        public string TextSubTitle
        {
            get
            {
                if (label2!= null) ///Means initially if control not loaded
                {
                    return label2.Text;
                }
                else
                {
                    return string.Empty;
                }
            }

            set
            {
                if (label2!= null)
                {
                    label2.Text = value;
                }
            }
        }

No comments:

Post a Comment