Hook into DataBind

Posted on 2004-11-24
Last Modified: 2012-08-14
My DataBind method is not being invoked.

The web control:
<myms:ToolbarButton runat="server" id="tbNext" datasource="<%# myDataSet %>"></myns:ToolbarButton>

Code behind:
namespace Somewhere
    public class ToolbarButton : Microsoft.Web.UI.WebControls.ToolbarItem
        public event EventHander DataBinding;
        public Control BindingContainer
            get { return this.ParentToolbar.NamingContainer }

        private object _ds = null;
        public object DataSource
            get { return _ds; }
            set { _ds = value; }

        public virtual void DataBind ( )
            if (DataBinding) { DataBinding(this, new EventArgs( )): }
            // ...

Problem is, DataBind never gets invoked. Even though the page contains a this.DataBind( ) call in Page_Load. All the other controls seem to get bound Ok, but not mine.

Extra details:
- Base class is NOT System.Web.UI.Control. It's one of the Microsoft internet web controls. As such I had to implement DataBinding and BindingContainer manually [took ages to figure out this was why my control wouldn't parse].
- On the parent page myDataSet is only initialised within Page_Load, so I can't use <%= ... %> .
- My class doesn't recieve events from the parent control/page, so I can't listen for something to happen. Within my constructor ParentToolbar isn't initialised yet, so I have no access to it or the parent Page.

Overriding Render is the best I can come up with to ensure that I perform a psuedo DataBind, but my DataSource is never called and therefore my DataSource is always null. Failing this a direct:

tbNext.DataSource = myDataSet;
tbNext.DataBind ( );

within the Page_Load of the parent page works, but it sure ain't pretty, considering that a this.DataBind ( ) is invoked anyhow (for other controls). Providing a separate Init ( ) method is an ugly alternative to a neat feature as DataBind, but it's a last resort.

Is there an Attribute I'm possibly missing, or is there some other means of hooking into DataBind( ) ?
Question by:_ys_
    LVL 9

    Author Comment

    Turns out the answer was in my post --> Base class is NOT System.Web.UI.Control.

    So I'm not in the control tree for the page. Of course my DataBind doesn't get invoked.

    Rolling out a custom implementation of Microsoft.Web.UI.WebControls.Toolbar and propogating the DataBind( ) onto my buttons did the trick. A simple this.DataBind( ) on the page therefore works a treat. No hack required.

    Accepted Solution

    Closed, 500 points refunded.

    Community Support Moderator
    Experts Exchange

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now