Search code examples
c#sharepointpropertiestextboxweb-parts

sharepoint webpart property disappear after a day (C#)


I developed a custom webpart with a webpart property with some textboxes and it works partially.

If I deploy the project, the written text in the property is away. This is ok.

But my MAIN Problem is, that about after one day the text in the property disappear is away. I use SharePoint 2010.

Here is my code:

[WebBrowsable(true), Category("category"), Personalizable(PersonalizationScope.Shared), WebDisplayName("Hello"), WebDescription("Description1")]
    public string hello
    {
        get { return _hello; }
        set { _hello = value; }
    }
    public static string _hello;

Solution

  • I solved it.

    I have the SharePoint-WebPart-Property defintion in the VisualWebPart1.cs and the build my "main" WebPart in the VisualWebPart1UserControl.ascx.cs.

    So the main problem was the connection between UserControl and the VisualWebPart1.cs. Here is the code for "connection" in the VisualWebPart1.cs:

            protected override void CreateChildControls()
        {
            VisualWebPart1UserControl control = (VisualWebPart1UserControl)Page.LoadControl(_ascxPath);
            control.WebPart = this;
            Controls.Add(control);
        }
    

    Then comes the Property WITHOUT static:

    public string _hello;
    [WebBrowsable(true), Category("category"), Personalizable(PersonalizationScope.Shared), WebDisplayName("Hello"), WebDescription("Description1")]
    public string hello
    {
        get { return _hello; }
        set { _hello = value; }
    }
    

    The next step I did is to get to the UserControl.ascx.cs and define a getter and setter to connect the UserControl.ascx.cs with VisualWebPart1.cs:

    public VisualWebPart1 WebPart { get; set; }
    

    Then you can initialize the variable with WebPart.hello