2010-09-13 26 views
1

我注意到當我重新啓動我的機器時,我開發的webpart的定製屬性返回到它們的默認值。Sharepoint Webpart定製屬性獲取服務器重啓時的默認值

這是正常行爲嗎?是服務器啓動時保存的屬性,還是有一些我缺少的參數。

謝謝。

編輯:代碼:

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(); 
      } 
     } 
} 
} 

EDIT2: 刪除靜態從田間地頭引發流動異常:

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) 
+0

這是不作爲的性質在持久化到數據庫正常行爲。一些示例代碼總是有幫助... – 2010-09-13 01:36:32

+0

好吧,我的代碼沒有什麼特別之處,請參閱更新。 – 0xFF 2010-09-13 08:14:19

+0

跳到我身上的一件事就是你繼承的類。看看這裏的評論 - http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webpartpages.webpart.aspx – 2010-09-13 11:03:48

回答

2

首先你不應該有

private static string _xmlFileUrl; 

它應該是

private string _xmlFileUrl; 

這個靜態變量將在IISRESET丟失 - 不會在農場工作,並有可能造成的所有排序的「線程安全」問題的潛力,如果使用多線程環境(如Web服務器),所以only use them if they are really needed

當SharePoint加載Web部件時(或者在您單擊工具部分中的保存/應用後),它使用反射來查找屬性([Browsable ...屬性)),然後序列化以加載/保存屬性的值到數據庫。其中一個失敗。

我會懷疑是有些問題的屬性 - try this one,直到它停止工作向後工作;)

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

刪除靜態現在提供了一個例外,請參閱編輯 – 0xFF 2010-09-13 15:55:43

+0

你知道靜態是什麼嗎? (它隱藏了屬性中的某些錯誤以及屬性的序列化不起作用的事實) - 您是否嘗試過更改屬性? – Ryan 2010-09-14 12:39:24