Search code examples
hierarchical-datadatabound-controls

HierarchicalDataBoundControl.PerformDataBinding not being called on postback


I am binding to a SiteMapDataSource (hierarchical).

I am overriding PerformDataBinding to grab the data from the datasource.

Everything works great on page load. But when I perform a postback anywhere on the page, the PerformDataBinding method does not get called, and in effect, not rendering any menu items (PerformDataBinding wasn't called).


Solution

  • No clue why this is happening, but I have a fix for it. Amazingly, every example of a HierarchicalDataBoundControl I could find (even from msdn) was doing this. However, here is a workaround.

        private bool dataBound = false;
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (this.Page.IsPostBack)
            {
                this.DataBound += delegate { dataBound = true; };
                this.Page.Load += delegate { if (!dataBound) DataBind(); };
            }
        }