سؤال

I have a web part that contains a number of custom properties. Only one of those customer properties shall not be set by the user. This property is an int, that represent a unique identifier.

I want to set this unique identifier the first time the web part is created. Certainly that means that if the identifier is 0, I set it to a new value that is incremented each time a value was used.

However, the value in the property doesn't get saved at all. It is available for the time the webpart is on the page, on refresh, the value will be set with the next unique value. (Actually the unique identifier is nothing else than an autoincremented number)

The property looks like this: [Personalizable()] public int InstanceId { get { return _instanceId; } set { _instanceId = value; } }

The property itself works fine when it gets set by the editor part applychanges method. I tried some samples with the GetLimitedWebPartManager, but it raises me an exception that the web part is not on the page. Most of those samples are used for changing an arbitrary webpart, the point here, I want to change the web part's property when it gets loaded.

So my question is, when the webpart's properties get saved and how? Is there a possibility to save the customer properties of the web part from inside the webpart?

Anyone an idea what i am doing wrong?

[Update] Thanks for hint djeeg. Tried this variant: (Is there a reason why new users are not allowed to use image tags?) http://www.freeimagehosting.net/image.php?8eb1892142.jpg and get the message that the webpart is not on the page (like before). I already searched for that exception and I am sure, this is the only WebPartManager instance that is used.

thanks

Holger

هل كانت مفيدة؟

المحلول

Try this

public class CustomWebPart : System.Web.UI.WebControls.WebParts.WebPart {
    private int _instanceId = 0;
    [Personalizable()] 
    public int InstanceId { 
        get { return _instanceId; } 
        set { _instanceId = value; } 
    }
    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);
        if (!Page.IsPostBack) { 
            if(InstanceId == 0) {
                InstanceId = 0; //set it to something   
                //this.WebPartManager.SaveChanges(this);
                using(SPLimitedWebPartManager manager = SPContext.Current.Web.GetLimitedWebPartManager(Page.Url, PersonalizationScope.Shared)) {
                    foreach(AspNetWebpart webpart in manager.WebParts) {
                        using(webpart) {
                            if(webpart == this) {
                                manager.SaveChanges(this);
                                manager.SaveChanges(webpart);
                            }
                        }
                    }
                }
            }
        }
    }
}

نصائح أخرى

Finally found the solution:

http://www.sharepointdev.net/sharepoint--development-programming/save-web-part-settings-45128.shtml

Here's a snippet:

        SPContext.Current.Web.AllowUnsafeUpdates = true;
        SPFile file = SPContext.Current.File;
        SPLimitedWebPartManager mgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared);

        for (int index = 0; index < mgr.WebParts.Count;index++ )
        {
            if (mgr.WebParts[index].ID == this.ID)
            {
               // Do your changes                   
               mgr.SaveChanges(mgr.WebParts[index]);
            }
        }
        SPContext.Current.Web.AllowUnsafeUpdates = false;

We can also use:

base.SetPersonalizationDirty();

As far as finding your web part in the SPLimitedWebPartManager, instead of looping through you can use the StorageKey property.

using(SPLimitedWebPartManager manager = SPContext.Current.Web.GetLimitedWebPartManager(SPContext.Current.Web.Url, PersonalizationScope.Shared))
{
MyWebPartType part = (MyWebPartType)manager.WebParts[((SPWebPartManager(WebPartManager.GetCurrencWebPartManager(Page))).GetStorageKey(this)];
...the rest of your code.

Or if you're inheriting your web part from Microsoft.SharePoint.WebPartPages.WebPart it's simply this.StorageKey.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى sharepoint.stackexchange
scroll top