随笔 - 58  文章 - 0  评论 - 277  阅读 - 46万 

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;

[WebBrowsable(false),

Personalizable(PersonalizationScope.User),

DefaultValue(0),

Category("CRM Factory"),

WebPartStorage(Storage.Personal)]

public int Factor

{

get

{

return _factor;

}

set

{

_factor = value;

}

}

 

void seeMoreButton_Click(object sender, EventArgs e)

{

Factor = Factor - 1;

if (Factor < 1)

{

Factor = 1;

}

 

SPSecurity.RunWithElevatedPrivileges(delegate()

{

SaveProperties = true;

});

GenerateTagCloud();

}

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   Allan.  阅读(1284)  评论(3编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示