For adding custom properties to our web part we need to do the following
1) Create property.
2) Decorate the property with the following attributes
WebBrowsable - To allow your property to be visible within SharePoint.
WebDisplayName- To provide display name to the property.
WebDescription- To provide description for that property.
Personalizable - To define the scope of it i.e either User or Shared through PersonalizationScope enumeration.
Let’s take a simple example wherein we have 2 properties defined, user will enter value for them and finally when the web part is rendered, we would be displaying their sum within the web part.
namespace AdditionWebPart
{
public class SumWebPart : WebPart{
private int firstVariable;
[WebBrowsable(true),
WebDisplayName("First Value"),
WebDescription("Enter value for first variable"),
Personalizable(PersonalizationScope.User)]
public int FirstVariable
{
get { return firstVariable; }
set { firstVariable = value; }
}
private int secondVariable;
[WebBrowsable(true),
WebDisplayName("Second Value"),
WebDescription("Enter value for second variable"),
Personalizable(PersonalizationScope.User)]
public int SecondVariable
{
get { return secondVariable; }
set { secondVariable = value; }
}
protected override void Render(System.Web.UI.HtmlTextWriter writer){
writer.Write(“The total is “ +this.calcTotal(this.firstVariable,this.secondVariable));
}
private int calcTotal(int a, int b){
return a + b;
}}}
No comments:
Post a Comment