代码之家  ›  专栏  ›  技术社区  ›  0xFF

Sharepoint Web部件自定义属性在服务器重新启动时获取默认值

  •  1
  • 0xFF  · 技术社区  · 14 年前

    这是正常的行为吗?这些属性是在服务器运行时保存的,还是缺少一些参数。

    非常感谢。

    编辑:代码:

    namespace TestWebpart
    {
        [ToolboxItemAttribute(false)]
        [XmlRoot(Namespace = "TestWebpart")]
        public class GraphWebpart : Microsoft.SharePoint.WebPartPages.WebPart
        {
            // Visual Studio might automatically update this path when you change the Visual Web Part project item.
            private const string _ascxPath = @"~/_CONTROLTEMPLATES/Test_Graph/TestWebpart/GraphWebpartUserControl.ascx";
    
            protected override void CreateChildControls()
            {
                ReloadElements();
            }
    
            protected void ReloadElements()
            {
                Controls.Clear();
                GraphWebpartUserControl control = (GraphWebpartUserControl)Page.LoadControl(_ascxPath);
    
                control.xmlDataUrl = XMLFileUrl;
    
                Controls.Add(control);
            }
    
            private static string _xmlFileUrl;
            [WebBrowsable(true),
            Personalizable(PersonalizationScope.Shared),
            DefaultValue(""),
            Description("xml"),
            DisplayName("xml"),
            WebDisplayName("xml")]
            public string XMLFileUrl
            {
                get { return _xmlFileUrl; }
                set { 
                    _xmlFileUrl = value;
                    ReloadElements();
                }
            }
    }
    }
    

    编辑2: 从字段中删除static会引发流动异常:

    Web Part Error: An error occurred while setting the value of this property: TestWebpart:XMLFileUrl - Exception has been thrown by the target of an invocation.
    Hide Error Details
    
    [WebPartPageUserException: An error occurred while setting the value of this property: Blue_Graph.GraphWebpart.GraphWebpart:XMLFileUrl - Exception has been thrown by the target of an invocation.]
      at Microsoft.SharePoint.WebPartPages.BinaryWebPartDeserializer.ApplyPropertyState(Control control) 
      at Microsoft.SharePoint.WebPartPages.BinaryWebPartDeserializer.Deserialize() 
      at Microsoft.SharePoint.WebPartPages.SPWebPartManager.CreateWebPartsFromRowSetData(Boolean onlyInitializeClosedWebParts)
    
    1 回复  |  直到 14 年前
        1
  •  2
  •   Ryan    14 年前

    首先你不应该

    private static string _xmlFileUrl;
    

    应该是的

    private string _xmlFileUrl;
    

    only use them if they are really needed

    当SharePoint加载web部件时(或在toolpart中单击“保存/应用”后),它将使用反射来查找您的属性(可浏览。。。属性),然后序列化以将属性的值加载/保存到数据库。其中一个失败了。

    我怀疑这是属性的问题- try this one

    [Browsable(true),
     Category("Miscellaneous"),
     DefaultValue(defaultText),
     WebPartStorage(Storage.Personal),
     FriendlyName("Text"),
     Description("Text Property")]