Difficulty determining when to re-bind web part controls

Tom Troughton asked on May 12, 2016 11:51

I have a web part exposed as a widget. It contains a few controls that need to be data bound. I only want to bind the data when not posting back. I therefore have the following code:

    public override void OnContentLoaded()
    {
        base.OnContentLoaded();

        SetupControl();
    }

    public override void ReloadData()
    {
        base.ReloadData();

        SetupControl();
    }

    protected void SetupControl()
    {
        if (StopProcessing)
        {
            // Do nothing
            this.Visible = false;
        }
        else if (!RequestHelper.IsPostBack())
        {
            // Bind my controls
        }
    }

This works find on the live site. However, in Page tab I'm finding that when an editor edits the widget's configuration, then saves and closes the widget dialogue, the page reloads and my data bound controls are now empty.

When debugging I can see that this re-load is a postback, but somehow my controls have lost their bindings. Has anyone experienced similar?

I've tried changing my SetupControl method to the following:

protected void SetupControl()
{
    if (StopProcessing)
    {
        // Do nothing
        this.Visible = false;
    }
    else if (!RequestHelper.IsPostBack() || !this.IsLiveSite)
    {
        // Bind my controls
    }
}

But for some reason the IsLiveSite property is true (and CMS.PortalEngine.PortalContext.IsDesignMode(this.PagePlaceholder.ViewMode) is false).

So how can I ensure that my control is only bound on initial page load, but changed to widget configuration also cause a re-binding?

Recent Answers


Brenden Kehren answered on May 12, 2016 13:58

Due to the dynamic loading of controls you need to bind the controls on every post back otherwise the viewstate will be incorrect and not load the controls and/or data. So I'd change your method to look like so:

protected void SetupControl()
{
    if (StopProcessing)
    {
        // Do nothing
        this.Visible = false;
    }
    else 
    {
        // set all your other properties

        if (!this.IsLiveSite || (!RequestHelper.IsPostBack() && this.IsLiveSite))
        {
            // bind your controls because you're in the admin interface
            // OR
            // because you're on the live site and it is the initial page load
        }
    }
}
2 votesVote for this answer Mark as a Correct answer

Tom Troughton answered on May 13, 2016 11:40

Hi Brenden,

Thanks for the response. Just to be clear, your condition (!this.IsLiveSite || (!RequestHelper.IsPostBack() && this.IsLiveSite)) is identical to (!this.IsLiveSite || !RequestHelper.IsPostBack()) which you'll see in the second part of my question is what I already have.

Howerver, the web part is reporting IsLiveSite to be true even when I'm editing in the Kentico UI. That's the problem.

At the moment I'm using the following condition which seems to be working fine:

(!RequestHelper.IsPostBack() || this.PagePlaceholder.ViewMode != CMS.PortalEngine.ViewModeEnum.LiveSite)

But I'm curious about why IsLiveSite would be true in cases when I'm not on the live site.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.