簡體   English   中英

使用“編輯Web部件”界面無需設置Webpart自定義屬性

[英]Set webpart custom properties WITHOUT using the Edit Web Part interface

我一直在努力嘗試找出答案,但要么我使用的術語不正確,要么就是找不到答案。 如果在未找到答案之前被問到並回答了,請給我一個鏈接,如果答案已經存在,我將不勝感激。

我有一個帶有自定義屬性(webpart類和用戶控件)的可視化webpart-所有這些都正確地裝飾了WebBrowsable(true),WebPartStorage(Storage.Shared)等屬性。 我也嘗試過Personalizable(Personalization.Shared)只是為了完整性。 我想做的是,不是要在標准的“編輯Web部件”界面中編輯這些WebPart屬性,而是要根據當前用戶是否是站點管理員來顯示針對它們的編輯控件。

這就是我的問題所在:當我從用戶控件設置自定義屬性時,設置不會保存。 我在webpart類的setter方法中設置了一個斷點,並驗證了確實已傳遞了一個值,但是當將webpart重新加載到頁面上時,設置將恢復為默認的空字符串。

我覺得我缺少一些基本步驟來保存這些設置。 以下是一些片段來演示我正在使用的代碼。 顯然,這不是完整的代碼,而只是說明我所處位置的相關部分。

SafetyTrackerControl.ascx.cs:

public partial class SafetyTrackerControl : System.Web.UI.UserControl
{
    public string EmployeeComments { get; set; }
    public SafetyTrackerWebpart ParentWebPart { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        txtEmployeeComments.Text = EmployeeComments;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        ParentWebPart.EmployeeComments = txtEmployeeComments.Text;
    }
}

SafetyTrackerWebPart.cs

[XmlRoot(Namespace = "SafetyTrackerSettings")]
public class SafetyTrackerWebPart : WebPart
{
    SafetyTrackerControl _SafetyTrackerControl;

    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        _SafetyTrackerControl = (SafetyTrackerControl)Page.LoadControl("~/_ControlTemplates/SafetyTracker/SafetyTrackerControl.ascx");
        _SafetyTrackerControl.EmployeeComments = EmployeeComments;
        _SafetyTrackerControl.ParentWebPart = this;
    }

    internal string _employeeComments;
    [WebBrowsable(true),
    WebDisplayName("Additional Comments"),
    WebPartStorage(Storage.Shared)]
    public string EmployeeComments
    {
        get { return _employeeComments; }
        set { _employeeComments = value; }
    }
}

如果您的Webpart繼承自Microsoft.SharePoint.WebPartPages.Webpart ,則可以嘗試使用SPWebPartManager類,並查看SaveChanges方法。

但是我認為正確的方法是創建從EditorPart類繼承的控件 ,並在那里決定用戶是否應該能夠編輯此以下屬性,或者在“編輯webpart”面板中不能。 您還應該重寫CreateEditorParts方法以返回自定義編輯面板。 哦,您還應該設置WebBrowsable(false)屬性,以便不會生成默認編輯器文本框。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM