Data Binding in ASP.NET

This article has been excerpted from book "A Programmer's Guide to ADO.NET in C#".

Web Forms provides many controls that support data binding. You can connect these controls to ADO.NET components such as a DataView, DataSet, or a DataViewManager at design-time as well as at run-time. Data binding in Web Forms works in the same way as it does in the Windows Forms with a few exceptions. For example, to bind a dataset to a DataGrid, you call the DataBind method of the DataGrid control. I'll discuss all this in a moment. First let's see some basics of data-bound controls. 

In this section I'll discuss data binding and how it works in ASP.NET

Data-Bound Controls

ASP.NET provides a rich set of data-bound server controls. These controls are easy to use and provide a Rapid Application Development (RAD) Web development. You can categorize these controls in two groups: single-item data-bound control and multi-item data-bound controls. 

You use the single-item data-bound controls to display the value of a single item of a database table. These controls don't provide direct binding with the data source. The highlighted part of table 7-20 shows an item of a database table. You use the Text, Caption, or Value property of these controls to show the data a field.

Mahesh

234, G. Road

12398

Mike

5443, NY

89433

AJ

P. Rd

54323

Table 7-20. An item

Examples of single-item data-bound controls are textboxes, buttons, labels, images, and so on.

You use the multi-item data bound controls to display the entire or a partial table. These controls provide direct binding to the data source. You use the DataSource property of these controls to bind a database table to these controls (see table 7-21).

Mahesh

234, G. Road

12398

Mike

5443, NY

89433

AJ

P. Rd

54323

Table 7-21. Multiple-item controls

Some examples of multi-item data-bound controls are DataGrid, ListBox, DataList, DropDownList, and so on. Figure 7-22 shows some of the data bound controls.

Figure7.22.gif

Figure 7-22. Data-bound control

In ASP.NET, you create these controls using a <asp:controlName> tag. Table 7-4 describes some common data-bound server-side controls.

Table 7-4 ASP .NET Data-Bound Controls

CONTROL ASP.NET CODE DESCRIPTION

DataGrid

<asp:DataGrid>

Display a database (through ADO .NET) in a scrollable grid format and supports selection, adding, updating, sorting, and paging

DataList

<asp:DataList>

Displays data in templates and style format

ListBox

<asp:ListBox>

Displays list box, which can be associated to ADO.NET data field to display data in a list format

DropDownList

<asp:DropDownList>

Displays DropDownList control, which can be used to display ADO.NET data source data in a drop down combo box format

CheckBox

<asp:CheckBox >

Display single check box, which can be connected to an item of the ADO.NET data source

CheckBoxList

<asp:CheckBoxList>

Display list of check boxes that can be connection to the to the ADO.NET data source

Repeater

<asp:Repeater>

Displays a templated data-bound list

TextBox

<asp:TextBox>

Displays a text box, which can be used to display ADO.NET using its Text property


Conclusion

Hope this article would have helped you in understanding Data Binding in ASP .NET. See other articles on the website also for further reference.

adobook.jpg
This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET.

Up Next
    Ebook Download
    View all
    Learn
    View all