The main method you might need to override when you create a basic Web Part is CreateChildControls, the problem is all the controls and its state will be retracted and recreated during each calling of the Web Part. There are lots situations that you want to keep the state after a customer click event on the front-end, and after the value changed you still want to save the value to Personalized Storage in order to loading the settings automatically next time. The customer properties can be used to store the personalized values, most of the times you want to change the customized values are edit the web part and change its Customer Properties in the Web Part Edit Panel, but how do you change it when you want to save any changes within the web part code?

The answer is SaveProperties property of the Web Part. In this case, your web part must be inherited from Microsoft.SharePoint.WebPartPages.WebPart. When the value changes, and you want to persistence into the user personalized storage, set SaveProperties to be true, this indicates that the web part manager should persistence the web part settings (Properties). Refer to the following code snippet:

private int _factor = 1;




Category("CRM Factory"),


public int Factor




return _factor;




_factor = value;




void seeMoreButton_Click(object sender, EventArgs e)


Factor = Factor - 1;

if (Factor < 1)


Factor = 1;





SaveProperties = true;




You can still use ViewState to store the Web Part state during the web part lifecycle, but it won't be able to hold the values until next login.

Reference: Microsoft.SharePoint.WebPartPages.WebPart.SaveProperties

The SaveProperties property is initially set to false at the beginning of page rendering. This property is useful only during page rendering. For example, this property has no effect when a Web Part is accessed through the object model.

The SaveProperties property can be used for Web Parts when their properties change after being rendered in a Web Part Page. After a property is changed, a WebPart class can set the SaveProperties property to true to specify that any changed properties will be saved. The value of the SaveProperties property is read by the Web Part infrastructure after the System.Web.UI.Control.UnLoad event of the Web Part occurs, at which point all property values are queried and stored in the SharePoint database. If this property is not set, the properties revert to their original values the next time the Web Part is instantiated.

The value of the SaveProperties property is ignored unless a Web Part is actively being rendered. For example, instantiating a new Web Part, changing a property value, and then setting SaveProperties to true has no effect. The SaveProperties property should be used by a Web Part that needs to save a property that has changed after being rendered. If a property is set externally, it is the responsibility of the external agent to either save the Web Part explicitly, or during a render cycle to set the SaveProperties property on behalf of the Web Part.

If the SaveProperties property is set on a static Web Part (a Web Part that is not located in a zone), or for a Web Part where the user does not have sufficient permissions to save changes (such as for an anonymous user), an exception is thrown when an attempt is made to save that Web Part. A Web Part should check the Permissions property before displaying a user interface for saving property values and setting SaveProperties to true.

posted on 2010-06-28 21:10  Allan.  阅读(1268)  评论(3编辑  收藏  举报