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.

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)
2
  • 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, 2010 at 8:41
  • I'm pretty sure yes, it only resets when I reboot the machine
    – Anonymous
    Sep 13, 2010 at 9:26

1 Answer 1

4

Your private _xmlFileUrl variable should not be static

4
  • deleting static from the fields gives an exception, please see the edit in the question
    – Anonymous
    Sep 13, 2010 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. Sep 13, 2010 at 16:26
  • Or perhaps Page is null Sep 13, 2010 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, 2010 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.