What is Asp.NET Two-Way DataBind Controls?
Asp.NET Two-Way DataBind Controls are the same controls that you encounter in the toolbox, but they have a new funcinality that is use them with the just know DataBindings of Windows Forms.

How to use the controls?
First add the ControlLibrary class library to you project and compile. Add the reference of that dll to your WebApplication project for example. Compile again, the controls should appear in the toolbox.
To use them is very simple. You add them like you add any other control from the toolbox. To use the new funcionality is very simple too, it will more simple if you already use the DataBindings of the Windows Forms. See the example:

class AClass
{
      public int AProperty{get;set;}
}

public APage : Page
{
      AClass c = new AClass();
      public void Page_Load{object sender, EventArgs e)
      {
            // here the c.AProperty has the default value 0
            this.txtTwoWay.DataBindings.Add("Text", c, "AProperty");

            // execute the DataBind of the all controls from the page
            this.ExecuteDataBind();
      }

      public void button1_Click(object sender, EventArgs e)
      {
             this.ExecuteDataBind();
             // here the c.AProperty has the value that the txtTwoWay Text is filled
      }
}


Some points to know, after you added the DataBind for the Text property in this line:

this.txtTwoWay.DataBindings.Add("Text", c, "AProperty");

you need to call the ExecuteDataBind(). This method is an extension method that you can call from a page or any control. So the line:

this.ExecuteDataBinds();

could be replaced like:

this.txtTwoWay.ExecuteDataBind();

Notice the ExecuteDataBind() again being called in the button click, this time it's setting the property of the class.

How the ExecuteDataBind() execute the real binds?
The ExecuteDataBind() method is a recursive method that calls the DataBind for any control. Why recursive? Imagine you need to call the ExecuteDataBind() for every control of a Page or UserControl, so you just need to call the Page's or UserControl's ExecuteDataBind() and every control will execute their DataBind();
Every control has an override of the DataBind, in this override they call the DataBindHelper that execute the bind for the properties.
The DataBindHelper verify if is not PostBack, if it is first the DataBindHelper set the value of the control and after set the value of the property. The opposite happens when it's not. This is bit logic, when the page is loading for the first time you don't have values in your controls, so you want that a value in the property set it. The opposite happens when it is PostBack (a button click for example).

What happens when the value is not valid?
When the value is not valid, for example when you enter a character in a TextBox Text that is bound to a int property, the initial value of that property will be set. For you understand better the Convert is this way:

1) The convert tries to convert property of the same type, for example if you bound a TextBox text it will try to convert to the same type (string)
2) If the convert fails the Convert verifies if it's a Nullable property or not. If it is the Convert will try to change the type for the Underlying property or for the property it's bound if it's not a Nullable prperty.
3) If the convert fails again it will set the default value for that property.

Can I create or add these funcionalities for my own controls?
Yes you can, and it is very simple. All you just to need it's copy the same code for any control and paste in your control.
You inherit your control and implement the IDataBinds. See the example below:

public class MyControlWithDataBindings : MyControl, IDataBinding
{
        private DataBindingCollection dataBindingCollection;

        public TextBoxBinding() : base()
        {
            this.dataBindingCollection = new DataBindingCollection();
        }

        protected override void OnDataBinding(EventArgs e)
        {
            HelperBinding.DataBind(this, this.Page);
            base.OnDataBinding(e);
        }
 
        // implementation of the IDataBind
        public DataBindingCollection DataBindings
        {
            get { return this.dataBindingCollection; }
        }
    }


Now compare with the TextBoxBinding:

public class TextBoxBinding : TextBox, IDataBinding
    {
        private DataBindingCollection dataBindingCollection;

        public TextBoxBinding() : base()
        {
            this.dataBindingCollection = new DataBindingCollection();
        }

        protected override void OnDataBinding(EventArgs e)
        {
            HelperBinding.DataBind(this, this.Page);
            base.OnDataBinding(e);
        }

        public DataBindingCollection DataBindings
        {
            get { return this.dataBindingCollection; }
        }
    }


The code is almost the same, the only change is the constructor!

What kind of test you did with it?
These controls are used in a project where I work, and result was very satisfied.

Last edited Feb 4, 2010 at 4:04 PM by MAGomes, version 4

Comments

No comments yet.