Wednesday, January 13, 2010

Custom Properties inside a SmartPart User Control

To add custom properties to a smartpart usercontrol is actually very easy.
First create a simple usercontrol and ensure the usercontrol works when added to a SharePoint SmartPart Web Part.
Then use the code (below) to add functionality within the usercontrol to manage the custom properties.


In this example when the user edit the SharePoint Page and "Modify Web Part", he/she will see on the right hand side of the screen a new custom property as part of the web part properties page.

When the user store a value in the smartpart Custom Property and return to the SharePoint page, he/she can click on the "cmdFetchValue" button and the code will return the value of the custom property into a textbox.

namespace testmysmartpart2

{
    [System.ComponentModel.Description("Custom Property Demo  
     SmartPart")]



    public partial class CustomProperties :  
                      System.Web.UI.UserControl
    {
        private string _customValue = null;
        [System.ComponentModel.Browsable(true),         
         System.ComponentModel.Description("Custom Value")]


        public string CustomValue
        {
        get { return _customValue; }
        set { _customValue = value; }
        }


        protected void cmdFetchValue_Click(object sender,
                                           EventArgs e)
        {
            TextBox1.Text = _customValue;
        }
    }
}

5 comments:

Anonymous said...

Could you please help me how do i insert a button as webpart custom field. Actually i want to add a Find button in webpart property and then fills the result into another webpart custom property.

thanks in advance.

Asim Nadeem said...

my email id is pixelsrain@gmail.com

zeeeman said...

this works great! so much simpler than referencing sharepoint dll etc. Thanks Johan.

Pepper_is said...

Thank you!

Unknown said...

Hi,Some software packages are designed for Apple Mac computers and may not be compatible for Web Design Cochin with Microsoft Windows operating system and vice versa. The second step would be to check the quality of Web pages already designed using the software.Thanks.....

Post a Comment