我正在使用安装了SharePoint powertools的Visual Studio 2010中的SharePoint 2010开发沙盒可视化Web部件。 Webpart会按预期的方式部署和工作,但属性不可编辑。我认为核心问题是沙箱中没有WebPartStorageAttribute,但是找不到如何创建具有可编辑属性的沙箱Webpart的指南。这有可能吗?

[ToolboxItem(false)]
[XmlRoot(Namespace="MyNamespace")]
public partial class MyWebPart: System.Web.UI.WebControls.WebParts.WebPart
{
     const string  defaultStartTime = "00:30:00";
     private string _startTime = "00:30:00";

    [Browsable(true)]
    [WebBrowsable(true)]
    [Category("Timer")]
    [Description("Start time to use if the user has not set a different one.")]
    [XmlElement(ElementName="StartTime")]
    [DefaultValue(defaultStartTime)]
    [FriendlyName("Start Time")]
    public string  StartTime
    {
        get
        {
            return _startTime;
        }
        set
        {
            _startTime = value;
        }
    }
...

上面的代码中缺少什么吗?是否可以创建一个可编辑的沙盒化Web部件,如果可以,如何完成?

最佳答案

我刚刚经历了这个过程,这就是我设法使其在沙盒解决方案以及365站点下运行的方法,这是完整的示例代码:

注意:我在用于Visual Studio的SharePoint Power Tools中使用Visual WebPart(沙盒)

ToolPartTest.ascx:

<h3>Selected List</h3>
<dd><asp:Literal runat="server" ID="list" /></dd>

TooLPartTest.ascx.cs:
[WebBrowsable(true), Personalizable(true)]
public string ListName { get; set; }

protected override void OnInit(EventArgs e)
{
  base.OnInit(e);
  InitializeControl();
}

protected void Page_Load(object sender, EventArgs e)
{

}

protected override void OnPreRender(EventArgs e)
{
  base.OnPreRender(e);

  list.Text = ListName ?? "(None)";
}

public override EditorPartCollection CreateEditorParts()
{
  return new EditorPartCollection(base.CreateEditorParts(),
    new[]
    {
      new CustomEditorPart
      {
        ID = ID + "_editorPart"
      }
    }
  );
}

ToolPart.cs
public class CustomEditorPart : EditorPart
{
  private DropDownList _list;

  protected override void OnInit(EventArgs e)
  {
    base.OnInit(e);

    _list = new DropDownList();
    _list.AppendDataBoundItems = true;
    _list.Items.Add("");
    _list.DataTextField = "Title";
    _list.DataValueField = "Title";
    _list.DataSource = SPContext.Current.Site.RootWeb.Lists;
    _list.DataBind();

    Controls.Add(new LiteralControl("List: "));
    Controls.Add(_list);

  }

  public override bool ApplyChanges()
  {
    EnsureChildControls();

    if (!string.IsNullOrEmpty(_list.SelectedValue))
      ((ToolPartTest)WebPartToEdit).ListName = _list.SelectedValue;

    return true;
  }

  public override void SyncChanges()
  {
    EnsureChildControls();

    var webpart = ((ToolPartTest)WebPartToEdit);
    _list.SelectedValue = webpart.ListName;

  }
}

关于.net - 如何在SharePoint 2010沙盒视觉WebPart上创建可编辑属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5717096/

10-11 03:56