开发者

Creating a custom EditorPart in SharePoint

开发者 https://www.devze.com 2023-03-31 04:19 出处:网络
The CreateChildControls method gets called before ApplyChanges. I am setting the edited property values to my Web Part, but they always end up with their default values.

The CreateChildControls method gets called before ApplyChanges. I am setting the edited property values to my Web Part, but they always end up with their default values.

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 looku开发者_运维知识库pMatches.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;
    }

I know this should be simple and straight forward, but this keeps alluding me for way to long now.


Your function

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

to modify an instance of editedWebPart you should pass it as reference like this

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

精彩评论

暂无评论...
验证码 换一张
取 消