3

我开发了一个带有一些文本框的 webpart 属性的自定义 webpart,它可以部分工作。

如果我部署项目,属性中的书面文本就没有了。还行吧。

但我的主要问题是,大约一天后,属性中的文本消失了。我使用 SharePoint 2010。

这是我的代码:

[WebBrowsable(true), Category("category"), Personalizable(PersonalizationScope.Shared), WebDisplayName("Hello"), WebDescription("Description1")]
    public string hello
    {
        get { return _hello; }
        set { _hello = value; }
    }
    public static string _hello;
4

3 回答 3

3

您不应该对 _hello 字符串使用静态声明符。此值与实例无关(因此是静态的),并且在应用程序池回收时(每天发生一次)将失去其值。删除静态并使用速记,你应该没问题:

[WebBrowsable(true),
 Category("category"),
 Personalizable(PersonalizationScope.Shared),
 WebDisplayName("Hello"),
 WebDescription("Description1"),
WebPartStorage(Storage.Shared)] 
public string Hello{ get; set;}
于 2012-08-21T11:56:18.187 回答
0

我解决了。

我在 VisualWebPart1.cs 中有 SharePoint-WebPart-Property 定义,并在 VisualWebPart1UserControl.ascx.cs 中构建了我的“主要”WebPart。

所以主要问题是 UserControl 和 VisualWebPart1.cs 之间的连接。这是 VisualWebPart1.cs 中“连接”的代码:

        protected override void CreateChildControls()
    {
        VisualWebPart1UserControl control = (VisualWebPart1UserControl)Page.LoadControl(_ascxPath);
        control.WebPart = this;
        Controls.Add(control);
    }

然后是没有静态的属性:

public string _hello;
[WebBrowsable(true), Category("category"), Personalizable(PersonalizationScope.Shared), WebDisplayName("Hello"), WebDescription("Description1")]
public string hello
{
    get { return _hello; }
    set { _hello = value; }
}

我所做的下一步是访问 UserControl.ascx.cs 并定义一个 getter 和 setter 以将 UserControl.ascx.cs 与 VisualWebPart1.cs 连接起来:

public VisualWebPart1 WebPart { get; set; }

然后你可以初始化变量WebPart.hello

于 2012-08-23T10:24:04.783 回答
-1

在我开发的所有 webpart 中,我也使用了WebPartStorage属性。我的猜测是,如果您不使用它,则该属性仅存储在内存中。因此,在 IISRESET/应用程序池回收后,您的属性值将丢失。

因此,您可以将代码更改为:

 [WebBrowsable(true), 
  Category("category"), 
  Personalizable(PersonalizationScope.Shared), 
  WebDisplayName("Hello"), 
  WebDescription("Description1"),
  WebPartStorage(Storage.Shared)] 
  public string hello 
  { 
      get { return _hello; } 
      set { _hello = value; } 
  } 
  public static string _hello; 
于 2012-08-21T10:17:51.227 回答