0

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 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;
    }

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

1 Answer 1

1

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());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.