Automatic Data Binding Controls

Introduction

While I was browsing some old projects, I discovered that in one of them I have approached an interesting topic of .NET framework. That topic was “Data Binding.”

Many of you know that the process of data binding is a complex one, and differs from web application to windows application, being optimized for each of them. From my point of view, data binding can be splinted in two branches: simple data binding and complex data binding.

You can speak about simple data binding when you are in the situation of binding a single value to a property of a control. Simple data binding operation is performed using <%# %>. The expression between data binding tags is evaluated only when the control’s data binding method is invoked. Here is an example of simple data binding:

Customer: <%# custID %>

where “Customer” is a label and “custID” is a public property.

Simple data binding works only with scalar values like strings and integers. Complex data binding is a different story. It works with any data type that implements IEnumerable interface. At the end of this article, you should have a panel that will act as a container for the child custom controls and it will automatically fill them with the corresponding values from its data source. For this to work, you need to create two custom interfaces. The first of them is IDataBound. The IDataBound interface defines the data types (DataTypes) of values that will be bounded to your control, the “BoundColumn” property, which will keep the name of the column used in binding process. The BoundValue property is used to store the new value of the control after the binding process. And finally, the IDataBoundInfo interface that contains the name of the table used in data binding. Please note that DataTypes enum contains at this time only data types that are found on System.Type. The reason I have added it to the code is that I might want to use a custom object as a data type for the data binding in the future.

DataTypes Definition


namespace MyControls
{
public enum DataTypes
{
Default,
String,
Integer,
DateTime,
Double
}
}

IDataBound Interface Definition


namespace MyControls
{
public interface IDataBound
{
DataTypes DataType { get; set; }
string BoundColumn { get; set; }
object BoundValue { get; set; }
bool SingleBind { get; set; }
}
}

IDataBoundInfo Interface Definition


namespace MyControls
{
public interface IDataBoundInfo : IDataBound
{
string TableName { get; set; }
}
}

BindingTextBox Control

After this short presentation of those two helper interfaces, it’s time to get down to business. I will create a text box custom control that will be called “BindingTextBox”. Here is the code for this control:


using System;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for BindingTextBox
/// </summary>
namespace MyControls
{
public class BindingTextBox : TextBox, IDataBoundInfo
{
/// <summary>
/// IDataBound members.
/// </summary>
private DataTypes _datatype = DataTypes.Default;
private string _boundcolumn;
private bool _singlebind;

/// <summary>
/// IDataBoundInfo members.
/// </summary>
private string _tablename;

public DataTypes DataType
{
get { return _datatype; }
set { _datatype = value; }
}

public string BoundColumn
{
get { return _boundcolumn; }
set { _boundcolumn = value; }
}

public virtual object BoundValue
{
get { return ControlHelper.ConvertValue
(_datatype, this.Text); }
set
{
if (value is DBNull)
this.Text = “”;
else
this.Text = value.ToString();
}
}

public bool SingleBind
{
get { return _singlebind; }
set { _singlebind = value; }
}

public string TableName
{
get { return _tablename; }
set { _tablename = value; }
}

}
}

At this time, please ignore the SingleBid property. I have used it for other purposes (data binding with multiple values) that I will not describe in this article.

ControlHelper Class

I have forgotten to mention about the ControlHelper class. If you remember, I made an observation regarding the DataTypes enum that at this time contains only data types that are found on System.Type, and I have give you an explanation also. The same thing goes for the ControlHelper class. It contains only one method used for conversion, but if you want some custom object, you could implement different methods for conversion. Here is the code for the ControlHelper class:


using System;

/// <summary>
/// Summary description for ControlHelper
/// </summary>
namespace MyControls
{
public class ControlHelper
{
public static object ConvertValue(DataTypes toType,
object value)
{
try
{
switch (toType)
{
case DataTypes.String: return Convert.ToString(value);
case DataTypes.Integer: return Convert.ToInt32(value);
case DataTypes.DateTime:
return Convert.ToDateTime(value);
case DataTypes.Double:
return Convert.ToDouble(value);
case DataTypes.Default: return value;
}
}
catch
{
return null;
}

return null;
}
}
}

I consider that both pieces of code are straight, simple, and self explanatory (BindingTextBox and ControlHelper).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read