SharePoint 2010 增加自定义属性
2013-04-22 15:24
387 查看
Adding Custom Properties to Visual Web Parts
Open the web part code file:Add the following code to the web part file:
private string propertyName = "Default Value";
[System.Web.UI.WebControls.WebParts.WebBrowsable(true),
System.Web.UI.WebControls.WebParts.WebDisplayName("Property Name"),
System.Web.UI.WebControls.WebParts.WebDescription(""),
System.Web.UI.WebControls.WebParts.Personalizable(
System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
System.ComponentModel.Category("Settings"),
System.ComponentModel.DefaultValue("Default Value")
]
public string PropertyName
{
get { return propertyName; }
set { propertyName = value; }
}
Which will result in the following screen when editing the web part :
Open the user control code file:
To be able to use this property comfortably we can add the following code to the user control:
public VisualWebPart1 WebPart { get; set; }
And add the following code to the user control code file:
protected void Page_Load(objectsender, EventArgs e)
{
this.WebPart = this.Parent asVisualWebPart1;
}
As a good friend of mine, Yoel Horovitz, pointed out to me (Thank you Yoel!):
The following code will not work in case an Output Cache is defined on the web part level, in contrast to page level caching (the following code resides on the Web Part cs file):
DO NOT USE:
protected override void CreateChildControls()
{
VisualWebPart1UserControl control = Page.LoadControl(_ascxPath) as
VisualWebPart1UserControl;
if (control != null)
{
control.WebPart = this;
}
}
Now you can access any custom property from the user control, like this
相关文章推荐
- sharepoint 2010 扩展调查字段属性(2)-增加图片支持
- sharepoint 2010 扩展调查字段属性(5)-增加图片支持-多选-查看投票结果
- sharepoint 2010 如何扩展webpart自定义属性边栏字段 custom webpart properties
- SharePoint 2010--为WebPart创建自定义属性
- sharepoint 2010 扩展调查字段属性(1)-增加图片支持
- sharepoint 2010 扩展调查字段属性(6)-增加图片支持-多选-根据权限控制是否允许用户多次评选
- sharepoint 2010 可视web部件 自定义属性 控件获取到自定义属性的值
- SharePoint 2010--为WebPart创建自定义属性
- sharepoint 2010 如何扩展webpart自定义属性边栏字段 custom webpart properties
- sharepoint 2010 在自定义列表的字段上增加功能菜单
- sharepoint 2010 扩展调查字段属性(4)-增加图片支持-多选
- sharepoint 2010 在自定义列表的字段上增加功能菜单
- [SharePoint 2010] 修改列表的“Add new item”为自定义
- 实用的SharePoint 2010 品牌化和自定义
- sharepoint 2010 配置自定义错误页面
- SharePoint 2010 自定义 字段 类型--------三级互联(省县市)
- SharePoint 2010 工作流解决方案:创建自定义网站工作流活动
- SharePoint 2010 品牌化和自定义--母版页
- 自定义对象的属性增加
- SharePoint 2010 自定义状态机工作流 (StateMachine Workflow) + InfoPath 实例part1 (工作流实现部分)