【问题标题】:Change property in webpart by code in sandbox solution通过沙盒解决方案中的代码更改 webpart 中的属性
【发布时间】:2013-03-02 08:15:14
【问题描述】:

这是我在 farm 解决方案中保存 Web 部件属性的方式:

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)
    {
        ((MyWebpartType) mgr.WebParts[index]).MyStringProperty = "Hello World!";
        mgr.SaveChanges(mgr.WebParts[index]);
    }
}
SPContext.Current.Web.AllowUnsafeUpdates = false;

工作正常。

现在我必须在 沙盒 解决方案中实现相同的目标,但没有可用的SPLimitedWebPartManager

那么如何通过沙盒解决方案 webpart 中的代码更改 webpart 属性?

【问题讨论】:

    标签: sharepoint sharepoint-2010 sandbox-solution


    【解决方案1】:

    找到解决方案,在 webpart 中调用 SetPersonalizationDirty() 会保存所有属性。

    【讨论】:

    • SPLimitedWebPartManager 发生了什么?
    • 您不能在沙盒解决方案中使用SPLimitedWebPartManager
    【解决方案2】:

    您可以定义自己的自定义 webpart 属性,如下所示:

     public partial class BannerSlider : System.Web.UI.WebControls.WebParts.WebPart
        { 
            [WebBrowsable(true),
            Personalizable(PersonalizationScope.Shared),
            WebDescription("My WebPart Property"),
            Category("Custom Properties"),
            WebDisplayName("My WebPart Property")]
            public string MyProperty
            {
                get { return __myProperty; }
                set { _myProperty= value; }
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                 this.MyProperty = "Hello World";
    

    【讨论】:

    • 感谢您的回答,但问题是如何保存属性值而不是如何创建属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 2012-06-20
    • 2013-12-02
    • 2011-03-20
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多