Navigate Up
Sign In
Supporters of Developer
Web

How To Create SharePoint 2010 custom visual webpart properties

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.

 

If you’ve created SharePoint 2010 classic web parts before, you know that you can easily define custom web part properties which your web part code-behind can consume.

2011-11-29-CustomWebpartProperties-01.png

With the popularity of SharePoint 2010 Visual Webpart, you’re probably interested in having custom properties defined there too.

There are few things different about exposing your custom properties in a Visual Web part.

Let’s see the steps involved:

  1. Add new or use existing Visual Web Part to your project, in our case called Webpart1
  2. Open the Webpart1.cs file which will look something like this:
  3. [ToolboxItemAttribute(false)]
        public class Webpart1: WebPart
        {
            private const string _ascxPath = @"~/_CONTROLTEMPLATES/[your ASCX].ascx";
            protected override void CreateChildControls()
            {
                Control control = Page.LoadControl(_ascxPath);
                Controls.Add(control);
            }
        }
    

    and add your custom property definition right below the CreateChildControls method, in our case the property will look something like this:

    [WebBrowsable(true),
            Category("Configuration"),
            Personalizable(PersonalizationScope.Shared),
            WebDisplayName("Friendly Display Name"),
            WebDescription("Values: Whatever value you need)]
            public string PropertyValue { get; set; }
    
    

    In here, the PropertyValue is the actual variable which will be later consumed by your web part user control.

  4. Update your CreateChildControls logic to look like this:
  5. protected override void CreateChildControls()
            {
                Control control = Page.LoadControl(_ascxPath);
                if (control!= null)
                {
                    ((Webpart1UserControl)control).WebPart = this;
                }
                Controls.Add(control);
            }
    

    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.

  6. Switch to your user control … Webpart1UserControl.ascx.cs
  7. and define public variable right below the class declaration:

    public Webpart1 WebPart { get; set; }
    

    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.

Enjoy!

This article was originally posted on Yaroslav's blog at http://www.sharemuch.com/

Categories: Web Parts; dev; MOSS; WSS; 2010

Comments

Searcher

Not a Visual Web Part

You did it for a Web Part Control, not a Visual Web Part Control

Posted 04-Jan-2012 by Searcher
parth

SharePoint 2010 custom visual webpart properties

Your articular is very helpful. I have one quick question: How do I use value of one of my custom properties inside "IF Statement" such as IF (List.Title == "value from custom properties"). where there can be more than one value. such as IF (list.title == " " || list.title == "" ....)

Posted 06-Feb-2012 by parth
Taffy Lewis

Error

I tried to do this sample, but it generated errors, :(

Posted 12-Jul-2012 by Taffy Lewis
pdanan

it works

it works. thanks

Posted 16-Jul-2012 by pdanan
Vincent

Unnecessary coupling

In your example you are coupling the webpart class to the user control class. A better solution would be to pass the property values to the control.

Posted 01-Nov-2012 by Vincent
Star U650

Star U650

In favor of my schoolwork reasons, I all the time used to download the video lectures from YouTube, as it is straightforward to fan-out from there.Star U89

Posted 25-Jul-2013 by Star U650
replica swiss movement watches

watches

Spring dress collection includes colorful apparels from calf dress tee length dresses, mini dresses, etc in different fabrics in different colors combinations and attractive, different styles, http://awatchesmall.co.uk wear colorful floral design? Looking for a comfortable striped dress? Need something in the plane? All your needs are met in line shops. Cold Spring dresses offered at a very low price of. Prepare to dress in style and fashion for this spring with an exclusive spring collections. Want to go for maxi dresses for casual looks? Need pants and Tees that spell for you? We are looking for some sexy lingerie collection? Accessories are to match with your apparels? All of these can simply enjoy the spring dress collection.

Posted 06-Apr-2014 by replica swiss movement watches
alksdsajdksfs

alksdsajdksfs

Jaquet Droz presented one of Replica Watches uk his latest creations: Ms. 8, watch, imagined, designed just for Replica omega watches women and inspiring for your project, from 8. the symbolic form of the ubiquitous Hublot big bang replica in the collection of the brand Swiss watch. This is a real trend. Nowadays, watch brands are more content to "slim down" watches her male-to-female.

Posted 17-Apr-2014 by alksdsajdksfs

Notify me of comments to this article

E-mail:
   

Add Comment

Title:

 
Comment:
Email:

   


Name:

 
Url: