Wednesday, July 7, 2010

How to add properties to visual web part

MSDN seems does not mention the details about how to add properties to visual web part. So I think it may help with sample code.

I found the link Referencing web part properties in SharePoint 2010 visual web part (I could not find the author of this post, and could not comment it), which really surprised me. Static variables suppose to be shared by different instances, which means we would not be able to specify different web part settings based on the same web part template in that way.  To confirm that, I did a quick test.  Below are the test code:





Below is the test result. We can see that all the web part instances share the same setting, which is wrong in most of the cases.

To fix that is quite easy:



Below is the final test result:


The source code are here:

--------------  VisualWebPart2.cs -------------

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 VisualWebPartProject1.VisualWebPart2
{
    [ToolboxItemAttribute(false)]
    public class VisualWebPart2 : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/VisualWebPartProject1/VisualWebPart2/VisualWebPart2UserControl.ascx";

        private string _LabelTestSettings = string.Empty;
        [WebBrowsable(true),
        Personalizable(PersonalizationScope.Shared),
        Category("User Settings"),
        DisplayName("test2"),
        WebDisplayName("test2"),
        WebDescription("test2")]
        public string LabelTestSettings
        {
            get { return _LabelTestSettings; }
            set
            {
                _LabelTestSettings = value;
            }
        }

        protected override void CreateChildControls()
        {
            VisualWebPart2UserControl control = (VisualWebPart2UserControl)Page.LoadControl(_ascxPath);
            control.LabelTestSettings = this.LabelTestSettings;
            Controls.Add(control);
        }
    }
}

--------------  VisualWebPart2UserControl.ascx.cs -------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace VisualWebPartProject1.VisualWebPart2
{
    public partial class VisualWebPart2UserControl : UserControl
    {
        public string LabelTestSettings { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = this.LabelTestSettings;
        }
    }
}


3 comments:

  1. Thanks for the tip! It works, except that the old value continues to be displayed in the web part until you refresh the page. I can't figure out why? Edit web part-->change custom field-->click ok-->see that old value is still displayed in the web part-->refresh page by clicking on breadcrumb-->now correct value appears.

    ReplyDelete
  2. Marc, thanks for your comment.

    As I know, the old value is cached by browser for performance reason. This is a feature, and we have to refresh the page to get the new value displayed.

    ReplyDelete
  3. Excellent Post buddy, really helped me out ...

    ReplyDelete