views:

101

answers:

1

I have noticed that the custom properties of a webpart I developed return to their default values when I reboot my machine.

Is that a normal behavior? are the properties saved as far as the server is up, or there is some parameters I am missing.

Thank you.

EDIT: code:

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: Deleting static from the fields throws the flowing exception:

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  A: 

First of all you should not have

private static string _xmlFileUrl;

it should be

private string _xmlFileUrl;

This static variable will be lost on IISRESET - won't work in a farm and has the potential to cause all sort of 'thread safe' issues if used multi-threaded environment (like a web server) so only use them if they are really needed.

When SharePoint loads a web part (or after you click Save/Apply in the toolpart) it uses reflection to find your properties (the [Browsable... attribute) and then serialization to load/save the value of the property to the database. One of these two is failing.

I would suspect that is some problem with the attribute - try this one and work backwards until it stops working ;)

[Browsable(true),
 Category("Miscellaneous"),
 DefaultValue(defaultText),
 WebPartStorage(Storage.Personal),
 FriendlyName("Text"),
 Description("Text Property")]
Ryan
deleting static gives an exception now, please see the edit
martani_net
Do you understand what static is doing (its hiding the fact that something is wrong in the attributes and the serialization of the properties is not working) - have you tried changing the attributes?
Ryan