You are on page 1of 18

Data Binding

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

• Data binding is the process that establishes a connection between


the app UI and the data it displays.

• 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.

• DataBind method will be used in the Page.Load event.


Single-Value Data Binding
• Single-value data binding is really just a different approach to dynamic text.
• 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:

<% # Request.Browser.Browser %>

• This would substitute a string with the current browser name (for example, IE).

• The following data binding expressions are all valid:

<% # GetUserName (ID) %>

<% # 1 + (2*20) %>

<% # “John ” & “Smith ” %>

• 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:

Note that this variable must be designated as public, protected, or internal,


but not private.
Page.Load event handler using some database lookup code

• 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:

<asp:Label id="lblDynamic" runat="server"><%# URL %></asp:Label>

• You can also use it for a check box caption:

<asp:CheckBox id="chkDynamic" Text="<%# URL %>" runat="server" />

• or you can use it for a target for a hyperlink:

<asp:Hyperlink id="lnkDynamic" Text="Click here!" NavigateUrl="<%# URL %>" runat="server" />

• or even as a picture:

<asp:Image id=“imgDynamic" Src=“<%# URL %>" runat="server" />


Problems with Single-Value Data Binding

• Putting code into a page’s user interface


• Over-enthusiastic use of single-value data binding can lead you to disregard that distinction and start coding function calls and even
operations into your page.
• If not carefully managed, this can lead to complete disorder.
• Code Fragmentation
• When data binding expressions are used, it may not be obvious to other developers where the functionality resides for different
operations.
• If the page code changes, or a variable or function is removed or renamed, the corresponding data binging expression could stop
providing valid information without any explanation or even an obvious error.
• No Design-Time Error Checking
• When you type a data binding expression, you need to remember the corresponding variable, property, or method name and enter it
exactly. If you don’t remember, you will receive an error.

You might also like