Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

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

Thank you.

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)
share|improve this question
    
looks Ok to me, are you sure that the _xmlFileUrl isnt accidentaly being reset by your code? All the examples I can find just use a get;set; which would essentially do what you are doing. –  Mauro Sep 13 '10 at 8:41
    
I'm pretty sure yes, it only resets when I reboot the machine –  Anonymous Sep 13 '10 at 9:26

1 Answer 1

up vote 4 down vote accepted

Your private _xmlFileUrl variable should not be static

share|improve this answer
    
deleting static from the fields gives an exception, please see the edit in the question –  Anonymous Sep 13 '10 at 15:56
    
I wonder if your call to ReloadElements() call inside your XmlFileUrl setter might be the cause. Attach debugger and step through? I wonder if perhaps your Controls collection is null at the time when SharePoint sets your XmlFileUrl as part of its deserialization, causing a NullReferenceExcention. –  Jaap Vossers Sep 13 '10 at 16:26
    
Or perhaps Page is null –  Jaap Vossers Sep 13 '10 at 16:27
    
Awesome, the properties have been reseting because the static in the fields. and for the exception, it was the Page that is null when the webpart initialize. I deleted all the ReloadElements() from the setters and just made one call to it from the PreRender() event, and everything is working PERFECTLY. many thanx again –  Anonymous Sep 13 '10 at 21:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.