Saturday, November 22, 2014

Adding a Web Part Property


1) Declare a private  variable
        private bool mySampleCheckBox = true;

2) Add the browsable web part property

        [System.Web.UI.WebControls.WebParts.WebBrowsable(true),
        System.Web.UI.WebControls.WebParts.WebDisplayName("Property Display Name on Web Part Edit"),
        System.Web.UI.WebControls.WebParts.WebDescription("Description of the property"),
        System.Web.UI.WebControls.WebParts.Personalizable(
            System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
        System.ComponentModel.Category("Category to include the property"),
        System.ComponentModel.DefaultValue(true)
        ]
        public bool SampleCheckBox
        {
            get { return mySampleCheckBox; }
            set { mySampleCheckBox =  value; }
       }

It would be much cleaner to use two using directives to refer System.ComponentModel and System.Web.UI.WebControls.WebParts and then, remove the repeated code above.

You could set the personalization scope to either Shared or User depending on your requirement.
     
Web part property types and their respective display controls in the web part properties pane are as below:

Property Type
Display
string
Textbox
bool
Checkbox
int
Textbox
float
Textbox
DateTime
Textbox
Enum
Dropdown

No comments:

Post a Comment