Creating Visual Web Part Properties

Source: Internet
Author: User

[To] http://www.codeproject.com/Articles/188019/Creating-Visual-Web-Part-Properties? Display = Mobile

Before reading this article, I assume you already know how to create your own WebParts in SharePoint and is looking for some way to create a property for that WebPart that you can easily Set and Get from your application, like the one on the image below:

But if not, please read this post regarding Developing Web Parts first to avoid confusion. But if you already know what to do, then continue on.

First, on your WebPart class (yellow highlight), and not the User Control class (green highlight), define your properties:

For this example, I will show you how to define a text, boolean, integer, date/time, and enumeration property:

[ToolboxItemAttribute(false)]public class Sample_Web_Part : WebPart{    // Visual Studio might automatically update    // this path when you change the Visual Web Part project item.    private const string _ascxPath =         @"~/_CONTROLTEMPLATES/Sample_Project/" +         @"Sample Web Part/Sample Web PartUserControl.ascx";    protected override void CreateChildControls()    {        Control control = Page.LoadControl(_ascxPath);        Controls.Add(control);    }    public static Boolean SampleBoolean;    [Category("Extended Settings"),    Personalizable(PersonalizationScope.Shared),    WebBrowsable(true),    WebDisplayName("Sample Boolean"),    WebDescription("Please Choose a Sample Boolean")]    public Boolean _SampleBoolean    {        get { return SampleBoolean; }        set { SampleBoolean = value; }    }    public static string SampleText;    [Category("Extended Settings"),    Personalizable(PersonalizationScope.Shared),    WebBrowsable(true),    WebDisplayName("Sample Text"),    WebDescription("Please Enter a Sample Text")]    public string _SampleText    {        get { return SampleText; }        set        {            // Sample Validation            Regex oRegEx = new Regex("[a-zA-Z]+");            if (!oRegEx.IsMatch(value))                throw new Microsoft.SharePoint.WebPartPages.                    WebPartPageUserException(                    "Please enter alphabeth characters only");            SampleText = value;        }    }    public static int SampleNumber;    [Category("Extended Settings"),    Personalizable(PersonalizationScope.Shared),    WebBrowsable(true),    WebDisplayName("Sample Number"),    WebDescription("Please Enter a Sample Number")]    public int _SampleNumber    {        get { return SampleNumber; }        set { SampleNumber = value; }    }    public static DateTime SampleDate;    [Category("Extended Settings"),    Personalizable(PersonalizationScope.Shared),    WebBrowsable(true),    WebDisplayName("Sample Date"),    WebDescription("Please Enter a Sample Date")]    public DateTime _SampleDate    {        get { return SampleDate; }        set { SampleDate = value; }    }    public enum CityEnum { Manila, Berlin, Auckland, Zurich };    public static CityEnum SampleDropDown;    [Category("Extended Settings"),    Personalizable(PersonalizationScope.Shared),    WebBrowsable(true),    WebDisplayName("Sample Drop Down"),    WebDescription("Please Choose a Sample DropDown")]    public CityEnum _SampleDropDown    {        get { return SampleDropDown; }        set { SampleDropDown = value; }    }}

If you notice, each property you create has attributes:

  • Category-This will group your property according to category. If not declared,"Miscellaneous"Will be used as the default.
  • Personalizable-How the WebPart is configured, which can be per-user (PersonalizationScope.User), Or for everyone (PersonalizationScope.Shared). For this example, we have chosen all users.
  • WebBrowsable-This will hide or show the property on the tool pane.
  • WebDisplayName-Label for the property.
  • WebDescription-Description for the property.

You might also notice that we have validation on the codes. You can add your own like the one onSampleTextProperty where we implemented a Regular Expression, or even inSampleNumber. It is by default validated by its type so you cannot save once it has illegal values.

So the final step is consuming that property in your WebPart; you can do it easily like :.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.