zoukankan      html  css  js  c++  java
  • 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(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
  • 相关阅读:
    Corn Fields 状压动归入门题
    codevs 2800 送外卖 floyd + Tsp
    互不侵犯 状压动归入门题
    跨终端电商平台的实现之手势效果(左右滑动)
    nodejs和树莓派开发以及点亮RGB的LED灯代码
    基于vue-cli搭了一个多页面应用的空脚手架
    About HTML
    【译】遗留浏览器中的表单
    Vue2的右键弹出菜单(vue-contextmenu)
    IMWEB 前端面试题汇总
  • 原文地址:https://www.cnblogs.com/ahghy/p/3035618.html
Copyright © 2011-2022 走看看