Browse DevX
Sign up for e-mail newsletters from DevX


Making Connections: Binding Controls to Custom Data Sources in .NET

Anyone can bind a DataGrid to a DataSet in .NET, but the fact that you can bind almost any control property to almost any suitable object property—even to custom objects or custom object collections—is far less widely known.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

inding data to user interface widgets is only part of the story in complex applications. Many analysis and design methods result in objects that contain both data and behaviour. DataSet and DataReader objects only capture the data of your application, not its specific behavior. Don't get me wrong. I have nothing against ADO, DataSets, or DataReaders—they are extremely valuable, and they're easy to data bind; however, if your application model contains objects with both data and behavior (methods) you still need to be able to bind those objects to the user interface of your application.

In this article you will see that connecting complex object architectures to Web and Form controls is just as straightforward as binding to DataSets or DataReaders. I've used Web-based (ASP.NET) examples because the programming model presents a number of unique challenges, but many of the examples and techniques apply equally well to both Web and Windows Forms applications.

Data Binding Basics
The simplest binding connects a control property value to the value of an object. That object may be a simple string attribute of the page, or a member of a complex object hierarchy. For example, probably the most common binding connects the text value of a control to the text of an objects property or field.

As an example consider an ASP label bound to a property of the form class. First, assume you have a MyForm.aspx page containing the presentation and a code-behind file called MyForm.aspx.cs. You'll often see hand-written ASP code using the syntax shown in the following example.

<asp:Label id=Label1 runat="server" Text='<%# MyProperty %>'></asp:Label>

When the ASP runtime builds the page, it evaluates the value of MyProperty and sets the Text property of the control to the string value of MyProperty by calling the ToString() method.

The companion code-behind page might look something like:

public class SimpleDataBinding : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; protected string MyProperty { get { return "MyProperty Value"; } } private void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { DataBind(); } } Web Form Designer generated code }

Note that the Text attribute of the control gets assigned to the property value only during the DataBind method call. Any previously existing value (such as in the case of a TextBox) is simply overwritten by the binding call. You might be wondering why the code implements the MyProperty member as a property rather than a simple field. It's because Visual Studio-generated code makes a call to the DataBinder.Eval() method rather than accessing the class member directly. The DataBinder.Eval method looks only for properties with matching names, and is unable to make use of fields declared in the code—in other words, using a field doesn't work. Compare the following example (generated by the Visual Studio IDE) to the first example page shown earlier.

<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "MyProperty") %>'> </asp:Label>

For the code to run properly, you must also alter the SimpleBinding class to make MyProperty public, because the Eval() method can't access protected properties. See the SimpleBinding2.aspx file in the downloadable sample code.

So, binding the value of a control to a form property is fairly straightforward but hardly satisfactory for a complex application. Applications often require many bound controls, and the properties aren't likely to be so easily accessible; they're far more likely to be scattered across a hierarchy of objects and embedded in collections.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date