adding custom property to webpart with a custom toolpart
I'm trying to add a custom property to my webpart as below:
[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[System.ComponentModel.Category("Settings")]
[WebDisplayName("RSS List Path")]
[WebDescription("")]
public string RSSListURL
{
get
{
if (_myListURL == null)
{
_myListURL = "http://server2003dev/dev/";
}
return _myListURL;
}
set { _myListURL = value; }
}
But within the webpart I am also overriding the GetToolParts() method as below with my own custom toolpart:
public override ToolPart[] GetToolParts() {
return new ToolPart[] { new RSSCountrySettings(), new WebPartToolPart() };
}
I need to display my custom toolpart (RSSCountrySettings) and my custom propery (RSS List Path) under the catergory Settings.
Any ideas how I do this, I ab开发者_如何学运维le to only get one but not both to display...?
You are not using the base class's toolparts. Try this instead:
public override ToolPart[] GetToolParts()
{
var result = new List<ToolPart>() ;
var toolparts = base.GetToolParts();
result.AddRange(toolparts.ToList());
result.Add(new WebPartToolPart());
return result.ToArray();
}
The reason you need to do this is that the base class generates a toolpart for your custom property. However, you do not allow it to add that toolpart to the toolpart collection. So, you must get the base class's toolparts as a collection, then add yours in there as well. That's the danger in overriding an existing method. Check this link out for more info
You have to add CustomPropertyToolPart to toolParts list. Like this:
public override Microsoft.SharePoint.WebPartPages.ToolPart[] GetToolParts()
{
List<ToolPart> list = new List<ToolPart>();
list.AddRange(base.GetToolParts());
// adds custom controls
result.Add(new WebPartToolPart());
// adds default controls for properties marked with [WebBrowsable(true)]
list.Add(new CustomPropertyToolPart());
return list.ToArray();
}
精彩评论