Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 366035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:35:40+00:00 2026-05-12T13:35:40+00:00

I have a custom control which inherits from System.Web.UI.Control and some of its properties

  • 0

I have a custom control which inherits from System.Web.UI.Control and some of its properties can be declaratively set using databinding expressions. e.g.

<foo:Foo runat="server" MyFoo="<%# this.GetFoo() %>" />

Now, when I do that I need to call .DataBind() on the control (or one of its parents) to evaluate these expressions.

What I would like to be able to do is detect if any properties were set this way and just automatically have the custom control call this.DataBind() after OnPreRender or there about.

So the question: how do I detect if databinding expressions are waiting to be executed?

I’m convinced that in some ControlBuilder or DataBindContext class lives the information needed to determine this. I’ve hunted around with Reflector and cannot seem to find it.

I should add, that I don’t want to pay the overhead of executing DataBind() if no direct properties have been assigned this way. This is why I’d like to detect before hand. This class is extremely light but I’d like the ability to declaratively set properties without needing any code behind.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:35:40+00:00Added an answer on May 12, 2026 at 1:35 pm

    Doing some deeper looking into ControlBuilder, I noticed that the compiled factory for each control instance will attach a DataBinding event handler when there are data binding expressions present. I’ve found that checking for this seems to be a very reliable method for determining if data binding needs to occur. Here is the basis of my solution to the problem:

    using System;
    using System.Reflection;
    using System.Web.UI;
    
    public class AutoDataBindControl : Control
    {
        private static readonly object EventDataBinding;
        private bool needsDataBinding = false;
    
        static AutoDataBindControl()
        {
            try
            {
                FieldInfo field = typeof(Control).GetField(
                    "EventDataBinding",
                    BindingFlags.NonPublic|BindingFlags.Static);
    
                if (field != null)
                {
                    AutoDataBindControl.EventDataBinding = field.GetValue(null);
                }
            }
            catch { }
    
            if (AutoDataBindControl.EventDataBinding == null)
            {
                // effectively disables the auto-binding feature
                AutoDataBindControl.EventDataBinding = new object();
            }
        }
    
        protected override void DataBind(bool raiseOnDataBinding)
        {
            base.DataBind(raiseOnDataBinding);
    
            // flag that databinding has taken place
            this.needsDataBinding = false;
        }
    
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
    
            // check for the presence of DataBinding event handler
            if (this.HasEvents())
            {
                EventHandler handler = this.Events[AutoDataBindControl.EventDataBinding] as EventHandler;
                if (handler != null)
                {
                    // flag that databinding is needed
                    this.needsDataBinding = true;
    
                    this.Page.PreRenderComplete += new EventHandler(this.OnPreRenderComplete);
                }
            }
        }
    
        void OnPreRenderComplete(object sender, EventArgs e)
        {
            // DataBind only if needed
            if (this.needsDataBinding)
            {
                this.DataBind();
            }
        }
    }
    

    This solution disables itself if no DataBinding event handler is attached or if the control is manually data bound (directly or via a parent).

    Note that most of this code is just jumping through hoops to be able to test for the existence of the event. The only reflection needed is a one-time lookup to get the object used as the key for EventDataBinding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to create a Custom Control which inherits from System.Web.UI.WebControls.Login and change
I have the following Problem: I create a custom control which inherits from an
I have a question about extending a custom control which inherits from UserControl. public
I have a custom control which displays results of some operations. It is hidden
We have a custom made server control which several properties defined in it. <cc1:DropdownCheck
We have a custom listener on our WCF solution, which inherits from ChannelListenerBase<IDuplexSessionChannel> .
I have implemented a custom designer class which inherits from DocumentDesigner. The standard Form
I have a custom control that inherits from a datagrid. I have to evaluate
I have a custom WinForms control (inherits from control, i.e. without user interface jsut
I have a custom server control which wraps a RadEditor (basically a textarea). I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.