Data Binding in ASP.NET

12 april 2021 om 10:00 by ParTech Media - Post a comment

The term ‘data binding in ASP.NET simply means that the data that is collected from a source is bound to the controls that provide the read and write connection to the data. Databases, XML files, flat files, etc are some of the data sources we are talking about here. In ASP.NET 1.x there was only one powerful data-binding technique that would reduce the length of the written code to bind the data that controls. However, in ASP.NET 2.0 you are provided with several features like sorting, filtering, automatic update, paging, data source controls, etc.

In this blog, we will understand what is Data binding and how to create it in ASP.NET.

Table of contents

  1. What is Data Binding?
  2. Introduction to Data Binding in ASP.NET
  3. Concept of Data Binding in ASP.NET
  4. How to create Data Binding in ASP.NET?
  5. Types Of Data Binding In ASP.NET
  6. Conclusion

What is Data Binding?

Data Binding can be simply understood as a process of establishing a connection between the Application User Interface and the Data. Now, one may wonder what is the difference between that and displaying the data on a UI. There is a big difference. The difference is if the data binding is not done correctly, then any changes to the data are not automatically reflected on the user interface.

Think of it this way. All the UI elements are continuously having an eye on the data variables assigned to them. The moment there is any change in the value assigned to the variables the UI elements are instructed to reflect the change instantly.

Data Binding in ASP.NET

Integration of the ASP.NET code with the results of properties, collection, method calls and database queries is made possible using data binding. Any programming burden surrounding the web control creation is relieved as you can combine the web control rendering with data-binding. The control contents can be populated from the SQL select queries or the stored procedures by using data-binding with ADO.NET and Web controls. There are two types of data-binding in ASP.NET namely, simple data-binding and declarative data binding. Some controls support both types while the rest support atleast simple data binding.

The following image illustrates the concept of Data Binding in ASP.NET:

  • Binding Target - The data is bound to this UI element or server control.
  • Binding Source - The business data is held in this application data variable.
  • Dependency Property - The data is bound to this attribute or property of the dependency object.
  • Dependency Object - It is the object which is associated with the binding target.
  • Source Property - The actual data is the value of this attribute or property of the source object.
  • Source Object - The business data is held in this object.

Now, if binding of text property is to be done with the employee name then the following are the relation,

  • TextBox - Target or Dependency Object
  • Text - Target property
  • Name - Property
  • Employee - Business Object

The propagation of data back and forth between the source and target is determined by the direction of binding. Any change in the business data is instantly reflected in the user interface element in case of one-way binding. However, the opposite is not permitted. One-way binding is useful in two cases, one when business data is read-only and two when the user interface is informative in nature.

When there is synchronization in the data at either end (source and target objects )automatically, it is called two-way binding. In two-way binding fully interactive UI forms are created, where back-end variables are updated instantly with changes in form fields.

Any change in the user interface element data is instantly reflected in the business object in case of one-way to source binding. Again the opposite is not permitted. This type of binding is useful when data calculation depends on the user input and frequent re-evaluation is needed.

How to create Data Binding in ASP.NET?

The below example is a simple demonstration of Data Binding in ASP.NET. A simple three control (slider, progress bar and text block) WPF application is created on UI.

Step 1

Click on Create a new project after opening Visual Studio.

Step 2

From the New Project Wizard Box select WPF App(.NET framework)

Step 3

Hit Next and then hit Create after configuring the basic details.

Step 4

Shell WEPF application will be created. In the created user interface, insert the slider, text block and the progress bar. Now, the value of the ProgressBar and TextBlock will be bound to the Slider control’s value.

Step 5

Add the below code as TextProperty of TextBlock

In the same way bind it to the ProgressBar.

Below you can see that the value in the text box and the progress bar change when the slider moves..

The Final Code will look like this -

Other types of data binding in ASP.NET

  • Simple Data Binding - In this, you attach the data source property of the control with any collection. This implements the dataset/datatable/IEnumerable interface.
  • Declarative Data Binding - DataSourceID is a necessary property of the controls that supports the more complex data binding. For data bound controls, separate data controls that are capable of managing data are used in the case of declarative data binding classes. This is different from simply iterating through a collection which happens in the case of simple data-binding. Functionalities like paging, sorting and editing data collections are implemented by data bound controls with the help of these data managers. For working with different data sources like Access databases, SQL Server, XML data etc. you have multiple data sources. As the controls already call the DataBind during PreRendering events in declarative data-binding, calling DataBind becomes optional. In ASP.NET 2.0, the process of rendering data has become way simpler for declarative data-binding.

Conclusion

This article gives you a peek into the concept of data-binding and its different classifications in ASP.NET. Data Binding is a necessity while developing any application and ASP.NET 2.0 reduces the workload of programmers and makes it necessary for them to explore more about it.

Nieuwste