在SharePoint中创建自定义EditorPart

时间:2011-08-25 09:20:23

标签: c# sharepoint

CreateChildControls之前调用ApplyChanges方法。我将已编辑的属性值设置为我的Web部件,但它们总是以默认值结束。

public Guid SelectedUId 
    {
        get
        {
            if (_ddlList == null)
            {
                return Guid.Empty;
            }

            return new Guid(_ddlList.SelectedValue);
        }
    }

    internal EditableLookupTableMatcher(string id)
    {
        this.Title = "Lookup Table Matcher";
        this.ID = string.Concat("LookupTableMatcher", "_", id);
    }

    protected override void CreateChildControls()
    {
        Controls.Clear();

        _ddlList = new DropDownList();
        _ddlList.ID = "ddlLts";

        _ddlList.DataTextField = "Value";
        _ddlList.DataValueField = "Key";

        Controls.Add(_ddlList);
    }

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

        _ddlList.DataSource = DataSource;
        _ddlList.DataBind();
    }

    protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
    {
        writer.Write(LookupTableName);
        writer.WriteBreak();

        _ddlList.RenderControl(writer);
        writer.WriteBreak();
    }

    public override bool ApplyChanges()
    {
        EnsureChildControls();
        var editedWebPart = WebPartToEdit as IReportFilterLookupMatches;

        if (editedWebPart == null)
        {
            return false;
        }

        SetValue(editedWebPart, LookupTableName, SelectedUId.ToString());

        return true;
    }

    public override void SyncChanges()
    {
        EnsureChildControls();

        var editedWebPart = WebPartToEdit as IReportFilterLookupMatches;

        if (editedWebPart == null)
        {
            return;
        }

        var UId = GetValue(editedWebPart, LookupTableName);
        for (var i=0; i < _ddlList.Items.Count; i++)
        {
            if (_ddlList.Items[i].Value == UId)
            {
                _ddlList.Items[i].Selected = true;

                break;
            }
        }
    }

    /// <summary>
    /// Gets the value of one property of an instance of type <see cref="IReportFilterLookupMatches"/> the editor is configured for.
    /// </summary>
    public static string GetValue(IReportFilterLookupMatches lookupMatches, string propertyName)
    {
        Guard.AgainstNull(lookupMatches, "lookupMatches");
        Guard.AgainstNullOrEmpty(propertyName, "propertyName");

        switch (propertyName)
        {
            case ReportFilterConstants.BUSINESS_HIERARCHY:
                return lookupMatches.BusinessHierarchyUId;

            case ReportFilterConstants.OPORTUNITY_STATUS:
                return lookupMatches.OpportunityStatusesUId;

            case ReportFilterConstants.REGION:
                return lookupMatches.RegionUId;

            case ReportFilterConstants.SPENDING:
                return lookupMatches.SpendTypeUId;

            case ReportFilterConstants.OPERATING_UNIT:
                return lookupMatches.OperatingUnitUId;
        }

        throw new InvalidOperationException(string.Format("The <{0}> property name is not configured in the LookupMatchesPropertyAdapter."));
    }

    /// <summary>
    /// Sets the value of one property of an instance of type <see cref="IReportFilterLookupMatches"/> rhe editor is configured for.
    /// </summary>
    private IReportFilterLookupMatches SetValue(IReportFilterLookupMatches lookupMatches, string propertyName, string value)
    {
        Guard.AgainstNull(lookupMatches, "lookupMatches");
        Guard.AgainstNullOrEmpty(propertyName, "propertyName");

        switch (propertyName)
        {
            case ReportFilterConstants.BUSINESS_HIERARCHY:
                lookupMatches.BusinessHierarchyUId = value;
                break;

            case ReportFilterConstants.OPORTUNITY_STATUS:
                lookupMatches.OpportunityStatusesUId = value;
                break;

            case ReportFilterConstants.REGION:
                lookupMatches.RegionUId = value;
                break;

            case ReportFilterConstants.SPENDING:
                lookupMatches.SpendTypeUId = value;
                break;

            case ReportFilterConstants.OPERATING_UNIT:
                lookupMatches.OperatingUnitUId = value;
                break;
        }

        return lookupMatches;
    }

我知道这应该是简单而直截了当的,但是现在这种方式一直在暗示着我。

1 个答案:

答案 0 :(得分:1)

你的功能

SetValue(editedWebPart, LookupTableName, SelectedUId.ToString());

要修改editedWebPart的实例,您应该将其作为参考传递,如此

SetValue(ref editedWebPart, LookupTableName, SelectedUId.ToString());