Pages

Search This Blog

Friday, February 11, 2011

Create Browsable Webpart Property

Sometimes there are requirements like we need to create a webpart which can be configured from the UI itself.
The solution is to create a webpart property which can be browse through UI so that user of the webpart can change the value accordingly and configure the webpart.
To add the Browsable Properties in the Toolpane of the webpart add the below code snippet  into your webpart code and deploy it.

public class TestWebPartProperty : WebPart
    {
        //WebBrowsable true - It will make the property to be browse in tool pane
        //Category - It will create the property section in the property tool pane
        //personilation - will share the personalized data to all users.
        //WebDisplay Name - It is the caption or lable of the property to be shown in property tool pane

        [WebBrowsable(true), Category("Custom Properties"), Personalizable(PersonalizationScope.Shared), WebDisplayName("CheckBox")]
        public bool Prop_CheckBox { get; set; }

        [WebBrowsable(true), Category("Custom Properties"), Personalizable(PersonalizationScope.Shared), WebDisplayName("TextBox")]
        public string Prop_TextBox { get; set; }


        protected override void CreateChildControls()
        {
            Label lblTextBoxValue = new Label ();
            Label lblCheckBoxValue = new Label ();

            lblCheckBoxValue.Text = Prop_CheckBox.ToString();
            lblTextBoxValue.Text = Prop_TextBox.ToString();

            this.Controls.Add(lblCheckBoxValue);
            this.Controls.Add(lblTextBoxValue);           
        }

After installing the webpart click on webpart click on Edit web part.

You will see your Custom Properties section in the Tool Pnae of the webpart. Now change the property to configure your webpart.


Now click on Ok button and it will show the custom properties value in the lables created.

No comments:

Post a Comment