SharePoint 2010 增加自定义属性

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( object sender, EventArgs e)
   {
    
        this .WebPart = this.Parent as VisualWebPart1 ;
    }

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

转载于:https://www.cnblogs.com/ahghy/archive/2013/04/22/3035618.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值