How to add custom property in custom webpart using VS 2012

来源:互联网 发布:黑猩猩智商知乎 编辑:程序博客网 时间:2024/06/03 01:39

How to add custom property in custom webpart using VS 2012

Sometimes we want to add some custom properties to custom web part, then we can edit the value of the custom properties. here is the picture

And the StorePortal is my custom properties, and I also set the default value of the custom properties.

Firstly, yopu should create a empty sharepoint project and add new item call webpart to the project.

Second, if you want to add cutom properties, just use the follow code:

        [WebBrowsable(true),              WebDisplayName("This is Name"),                             // The display name              WebDescription("This is Description, e.g. 'blog1#blog2'"),  // the description of the custom properties            Personalizable(PersonalizationScope.Shared),              Category("This is new category")]                           // to display the properties in a new section          public string BlogWeb { get; set; }  

 

the code is following:

    [ToolboxItemAttribute(false)]    public class render : WebPart    {        #region Custom web part properties        // blog web related url, e.g. "/blog"                [WebBrowsable(true),            WebDisplayName("Blog web related url"),            WebDescription("Blog web related url, e.g. 'blog1#blog2'"),            Personalizable(PersonalizationScope.Shared),            Category("StorePortal")]        public string BlogWeb { get; set; }        // the number of show tags, e.g. 10            [WebBrowsable(true),            WebDisplayName("The number of tags"),            WebDescription("The number of tags, e.g. 1"),            Personalizable(PersonalizationScope.Shared),            Category("StorePortal"),            DefaultValue(NumberLimitDefaultValue)]        public uint NumberLimit { get { return this.numberLimit; } set { this.numberLimit = value; } }        #endregion        #region private fields        private const uint NumberLimitDefaultValue = 10;        private uint numberLimit = NumberLimitDefaultValue;        #endregion        protected override void CreateChildControls()        {            //try            //{            // you can cutom to coding, and if you want to call the custom properties. For example: this.BlogWeb            //}            //catch { }        }}


 

 

 

原创粉丝点击