Data Binding
Data Binding
Data binding
• In ASP.NET, the term Data Binding implies binding the controls to data
that is retrieved from the data source and hence providing a read or
write connectivity between these controls and the data, that they are
bound to.
• These data sources can be one of databases, xml files or even, flat
files.
• Data binding establishes a connection between the application UI and
business logic.
• Two types:
• Single value
• Repeated value
Single value binding
• Single value data binding implies, binding of a single value or a single record, say, an employee's record.
• You can use single-value data binding to add information anywhere on an ASP.NET page.
• You can even place information into a control property or as plain text inside an HTML tag.
• Single-value data binding allows you to take a variable, a property, or an expression and insert it dynamically
into a page
• To use it, you add special data-binding expressions into your .aspx files. These expressions have the following
format:
<%# expression_goes_here %>
• For example, if you have a public or protected variable named Country in your page, you could write the
following:
<%# Country %>
• When you call the DataBind() method for the page, this text will be replaced with the value for Country (for
example, Spain).
Similarly, you could use a property or a built-in ASP.NET object as follows:
<%# Request.Browser.Browser %>
A Simple Data-Binding Example
• Consider a variable TransactionCount defined in your Page class
public partial class SimpleDataBinding : System.Web.UI.Page
{
protected int TransactionCount;
}
• Assume that this value is set in the Page.Load event handler
protected void Page_Load(object sender, EventArgs e)
{
TransactionCount = 10;
this.DataBind();
}
• Two actions take place in this event handler: the TransactionCount variable is set
to 10, and all the databinding expressions on the page are bound.
• To add your expression, find the tag for the Label control. Modify the text inside
the label as shown here:
<asp:Label id="lblDynamic" runat="server" Font-Size="X-Large">
There were <%# TransactionCount %> transactions today.
I see that you are using <%# Request.Browser.Browser %>.
</asp:Label>
A Simple Data-Binding Example
• This example uses two separate data-binding expressions
• The first data-binding expression references the TransactionCount
variable, and the second uses the built-in Request object to
determine some information about the user’s browser.
• Output looks like this:
Repeated-Value Data Binding
• Repeated-value data binding allows you to display an entire table (or just a single field
from a table).
• Unlike single-value data binding, this type of data binding requires a special control that
supports it.
• Typically, this is a list control such as CheckBoxList or ListBox, or other types of Data
controls such as a GridView
• A control that supports repeated-value data binding provides a DataSource property.
• Repeated value binding can also be used to bind data from a collection or an array.
• Following are the controls that supports repeated value binding:
1. ListBox, DropDownList, CheckBoxList, and RadioButtonList
2. HtmlSelect
3. GridView, DetailsView, FormView, and ListView
Data Binding with Simple List Controls
Steps:
• Create and fill some kind of data object that supports collections such
as an array, basic ArrayList and Hashtable,List and Dictionary
collections, and the DataTable and DataSet objects
• Link the object to the appropriate control.Set a couple of properties,
including DataSource.
• Activate the binding using DataBind() method
A Simple List-Binding Example
• Add a ListBox control to a new web page. Then use the Page.Load event handler to create a
strongly typed List collection to use as a data source:
List<string> fruit = new List<string>();
fruit.Add("Kiwi");
fruit.Add("Pear");
fruit.Add("Mango");
fruit.Add("Blueberry");
fruit.Add("Apricot");
fruit.Add("Banana");
fruit.Add("Peach");
fruit.Add("Plum");
• Now link this collection to the ListBox control:
ListBox1.DataSource=fruit;
• To activate the binding, use the DataBind() method:
this.DataBind();
or
ListBox1.DataBind() to bind just the ListBox control.
Data Binding with ADO.NET
• When you’re using data binding with the information drawn from a database, the data-binding process takes
place in the same three steps. First you create your data source, which will be a DataReader or DataSet
object.
• The next example creates a DataSet and binds it to a list(instead of this example you can also write example
given in prac no.8c )
// Define a DataSet with a single DataTable.
DataSet ds = new DataSet();
ds.Tables.Add("Users");