NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Web Forms Data Binding

The Web Forms page framework provides an easy-to-use, flexible means of binding controls to information in a data store. The approach used in Web Forms allows you to bind any control property to information in almost any kind of data store, from the simplest to the most sophisticated, and gives you virtually complete control over how data moves from the data store to the page and back again.

Data binding in Web Forms works somewhat differently than it does in Windows Applications and differently than in previous versions of products such as Visual Basic®. This topic provides background information that you will find helpful when designing data binding for your Web Forms pages.

Data Classes in Web Forms

Data is made available to Web Forms pages in the form of classes that expose the raw data as properties and that typically define methods for updating the underlying data store. These classes incorporate a data adapter to interact with a database, the data sets themselves, properties to retrieve and set the data in the data set, and methods to write the data back to a database. You can create instances of the resulting data classes and use their members to retrieve, navigate, and update the data.

In fact, Web Forms pages can take advantage of virtually any publicly available information. Simple controls — those that bind to a single value — can bind to public property on the page, whether from another control or the page itself.

Complex controls that include embedded controls (such as the Repeater, DataList, and DataGrid Web controls) can bind to any structure that implements the ICollection interface. This interface guarantees that the data class provides a basic means of data access and navigation. You can use a structure as simple as an array by implementing it using a class that incorporates the ICollection interface, such as the ArrayList class.

The concept of what "data" means for data binding in the Web Forms page framework is therefore very broad. This gives you great flexibility in what you can bind to as data. Of course, some data classes provide considerably more functionality than others, which you can take advantage of in your own code. But the basic functionality of data binding in Web Forms is not tied to a specific level of data class functionality. The result is a clean distinction between data and data binding, and as a consequence, flexibility for you.

Binding Controls to Data

You establish data binding for controls with these basic steps:

The syntax of a data binding expression is simple. In the Web Forms page (.aspx) file, the expression is embedded as part of the property in the control declaration. A data binding expression used to set the Text property of a Label Web control might look like the following:

<asp:Label runat=server Text='<%# CustList(0).FirstName %>'/>

The data binding expression itself is delimited with <%# and %> tags. Within the tags, the expression refers to a public source of information. If the expression references a member of a data class, the expression must specify as much of the class hierarchy as necessary to evaluate to a single data item. You can optionally provide formatting information to specify an appropriate string format for the data being bound.

In complex controls such as the Repeater, DataList, and DataGrid controls, the control can act as a container for other controls that actually display data. For example, a Repeater control might contain a template with a Label control to display customer names. When the Repeater runs, it repeats the contents of its template once for every record in the customer data class, and each Label then displays the next customer name.

The data binding expression in that case is similar to that for single controls. The following example shows the complete definition of the Repeater control, including the template with the Label control.

<asp:repeater id="MyRepeater" DataSource="CustomersClass" runat=server>
   <template name="itemtemplate">
      <asp:Label ID="Label1" runat="server"
          Text="<%#Container.CurrentCustomer.Name%>">
      </asp:Label>
   </template>
</asp:repeater>

Performing Data Binding

The Web Forms page framework does not automatically evaluate data binding expressions. Instead, the expressions are evaluated only when the DataBind method is called. This method is supported by each control and by the page as a whole; when the method is called for the page, it is cascaded through the page hierarchy, calling the DataBind method for each control.

The advantage of having an explicit method to evaluate the data binding expression is that you can control when the controls are loaded with data. Typically, you do this at two times:

Updating Data Sets

A majority of Web application data scenarios do not involve data update. Instead, the Web page displays data for information only or displays it with hyperlinks. The binding performed with data binding expressions is therefore one-way — it reads data from a data class and loads it into control properties. If you want to write data back to a data store, you must do so yourself.

A common scenario is to update the data store in an event handler. You extract the values from the controls and then update the data store. If the data source is an ADO+ data set, you update the current record and then call an ADO+ update method, which causes the data to be written through to the underlying database.

See Also

Web Forms Server Controls