Sunday, December 11, 2011

How To Make SharePoint 2010 customized made graphic webpart properties


If you have designed SharePoint 2010 vintage web parts before, you know that you can easily determine customized made web element qualities which your web element code-behind can eat.


With the reputation of SharePoint 2010 Graphic Webpart, you are probably interested in having customized made qualities identified there too.
There are few items different about revealing your customized made qualities in a Graphic Web par

Let’s see the steps involved:

Add new or use existing Visual Web Part to your project, in our case called Webpart1
Open the Webpart1.cs file which will look something like this:

    01[ToolboxItemAttribute(false)]
    02    public class Webpart1: WebPart
    03    {
    04        private const string _ascxPath = @"~/_CONTROLTEMPLATES/[your ASCX].ascx";
    05        protected override void CreateChildControls()
    06        {
    07            Control control = Page.LoadControl(_ascxPath);
    08            Controls.Add(control);
    09        }
    10    }
    and add your custom property definition right below the CreateChildControls method, in our case the property will look something like this:
    1[WebBrowsable(true),
    2        Category("Configuration"),
    3        Personalizable(PersonalizationScope.Shared),
    4        WebDisplayName("Friendly Display Name"),
    5        WebDescription("Values: Whatever value you need)]
    6        public string PropertyValue { getset; }
    In here, the PropertyValue is the actual variable which will be later consumed by your web part user control.
  1. Update your CreateChildControls logic to look like this:
  2. 1protected override void CreateChildControls()
    2        {
    3            Control control = Page.LoadControl(_ascxPath);
    4            if (control!= null)
    5            {
    6                ((Webpart1UserControl)control).WebPart = this;
    7            }
    8            Controls.Add(control);
    9        }
    This piece here will pass in the properties every time the control is reloaded. In here, the only change you’ll need to make is to rename Webpart1UserControl to the [name of your webpart]UserControl. This will reference the ASCX used for the Visual Webpart.
  3. Switch to your user control … Webpart1UserControl.ascx.cs
  4. and define public variable right below the class declaration:
    1public Webpart1 WebPart { getset; }
    Here the Webpart1 is referencing the actual web part class file (not ASCX).
That’s all, in your code you can get a hold of the web part property value by using this: this.WebPart.PropertyValue, where the PropertyValue is the name of the variable we’ve chosen earlier.








SPONSORS:

No comments:

Post a Comment