3
votes

I have a sharepoint webpart that uses some webpart properties (string in this case). The properties all work as I would like however say I make a change to our webpart and deploy to the server, the existing properties are lost.

After breif reading I believe it may be related to my property definition:

    public static string Exclusions;
    [Category("Extended Settings"),
    Personalizable(PersonalizationScope.Shared),
    WebBrowsable(true),
    WebDisplayName("Library Exclusions"),
    WebDescription("Enter any Libraries to exclude. Use '|' to separate.")]
    public string _Exclusions
    {
        get { return Exclusions; }
        set
        { Exclusions = value;}
    }

I am wondering if I should remove the "static" from here. However when I go to do so I can no longer use this property as I have been:

protected override void OnPreRender(EventArgs e)
        {

            ((HiddenField)this.FindControl("DocumentLibraryListingHiddenWebPartProperties")).Value = DocumentLibraryListing.DocumentLibraryListing.Exclusions;

        }

Any suggestions on what to do here?

1

1 Answers

3
votes

It turns out that the setting/property being lost was related to the string being static.

I changed my property to:

public string Exclusions;
[Category("Extended Settings"),
Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("Library Exclusions"),
WebDescription("Enter any Libraries to exclude. Use '|' to separate.")]
public string _Exclusions
{
    get { return Exclusions; }
    set
    { Exclusions = value;}
}

In addition, I had to do the following in the CreateChildControls method:

protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath);
            if (control!= null)
            {
                ((MyUserControl)control).WebPart = this;
            }
            Controls.Add(control);
        }

Also, in the ascx.cs file for the webpart I had to add this right below the class definition:

public MyWebPart WebPart { get; set; }

Finally I was able to access the values on the OnPreRender event using:

this.WebPart.Exclusions

For more information please see this site: NothingButSharepoint.com