Friday, September 5, 2008

WebPart Custom Properties in ASP.Net

Scenario:
You want to add a custom property for the ASP.Net WebPart

Solution:

private string _sZipCode;
[Personalizable(PersonalizationScope.Shared),
WebBrowsable, WebDisplayName("Select the zip code"),
WebDescription("Use this property to set zipcode")]
public string ZipCode
{
get {return _sZipCode; }
set {_sZipCode = value; }
}

protected override void RenderContents(HtmlTextWriter output)
{
RenderChildren(output);
// Securely write out HTML
output.Write("
Text Property: " + SPEncode.HtmlEncode(Text));
}
Note:
This solution only works if you are inheriting from System.Web.UI.WebControls.WebParts.WebPart.

If you are inheriting Microsoft.SharePoint.WebParts.WebPart class and want to have custom properties, then read the article below.

Article:
http://msdn.microsoft.com/en-us/library/ms948927.aspx

0 comments: