Professional Documents
Culture Documents
R.Sheeba M.Sc.,M.Phil.,
Assistant professor,
Department of computer science
Madurai sivakasi nadars pioneer Meenakshi
women’s College, poovanthi
Introduction to Data Binding
• When the data changes its value, the UI elements that are bound to
the data will also change.
Types of ASP.NET Data Binding
There are actually two types of ASP.NET data binding.
Single-Value or “Simple Data Binding”
• This type of data binding is used to add information anywhere on an
ASP.NET page.
• You can use single-value data binding to add information anywhere on
an ASP.NET page.
• Single-value data binding allows you to take a variable, a property, or an
expression and insert it dynamically into a page.
• To use single-value binding, you must insert a data binding expression
into the markup in the .aspx file (not the code-behind file).
Repeated-Value or List Binding
• This type of data binding is much useful for handling complex labels, such as
displaying an entire table or all the values from a single field in a table.
• Repeated-value data binding works with the ASP.NET DataGrid and List
controls such as CheckBoxList or ListBox.
• To use repeated-value binding, you link one of these controls to a datasource
(such as a field in a data table).
• It supports repeated-value data binding if it provides a Datasource property.
How Data Binding Works
• Data binding works little differently depending on whether you are using
single-value or repeated-value binding.
• In single-value binding, a data binding expression is inserted right into the
display portion of the .aspx file.
• In repeated-value binding, data binding in configured by setting the
appropriate control properties. (Ex:- Load event for the page)
DataBind() Method
• After specifying the data binding, we need to activate it. This is accomplished by calling the
DataBind method.
• When you call DataBind(), the control automatically creates a full list using all the corresponding
values. This saves you from writing code that loops through the array ordata table and manually
adds elements to a control
• The DataBind() method is basic piece of functionality supplied in the control class. It
automatically binds a control and any child controls that it contains.
DataBind() Method (cntd…)
• With repeated value binding, you can use the DataBind method for the specific list control you are using.
• Alternatively, you can bind the whole page at once by calling the DataBind method for the current page.
• Once you call this method, all the data binding expressions in the page are evaluated and replaced with the
specified value.
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:
• This would substitute a string with the current browser name (for example, IE).
• These data binding expressions are placed in the HTML code for your .aspx file.
A Simple Data Binding - Example
• The below program shows a simple example of single-value data binding.
• It starts with a variable defined in your Page class, which is called
TransactionCount:
• Two actions actually take place in this event handler: the TransactionCount variable
is set to 10, and all the data binding expressions on the page are bound.
• To make this data binding accomplish something, you need to add a data binding
expression.
• This example uses the this keyword to refer to the current page
Source view in the web page designer
• To add your expression, find the tag for the Label control. Modify the text
inside the label as shown here
• This example uses two separate data binding expressions, which are inserted along with
the normal static text. 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. When you run this page.
• The data binding expressions have been automatically replaced with the appropriate
values.
The result of data binding
Simple Data Binding with Properties
• You can also use single-value data binding to set other types of information
on your page, including control properties.
• To do this, you simply have to know where to put the data binding
expression in the web page markup
• For example, consider the following page, which defines a variable named
URL and uses it to point to a picture in the application directory
• You can now use this URL to create a label, as shown here:
• or even as a picture: