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);
}
You will see your Custom Properties section in the Tool Pnae of the webpart. Now change the property to configure your webpart.
No comments:
Post a Comment