320 likes | 524 Views
Objectives In this lesson, you will learn to: Identify the different types of data binding Bind server controls to data sources Create templates for server controls Access data from a database by using ADO.NET Edit bound controls. Introduction to Data Binding
E N D
Objectives In this lesson, you will learn to: • Identify the different types of data binding • Bind server controls to data sources • Create templates for server controls • Access data from a database by using ADO.NET • Edit bound controls
Introduction to Data Binding • Data binding helps you develop such applications where controls can be bound to the values stored in a data source. • Some Web server controls can be bound to a data source such as an array list or a collection, which contains static data, while some Web server controls can be bound to a data source such as a database table, which contains dynamic data. • The properties of server controls are set to a specific data source and when the Web application is executed, the control extracts and displays data from the data source.
Introduction to Data Binding (Contd.) • The syntax for data binding is as follows: <%# expression %> • The DataBind() method of the control is used to bind it to a data source.
Just a Minute… • What is the function of the DataBind() method? • Write the syntax to bind a Label control to a data source and display the values from the data source at run time.
Problem Statement 2.D.1 A Web application needs to be created to accept the product name and quantity of a toy from a customer. After the customer has entered the product name and quantity, the application needs to display the discounted price of the product to the customer (the company is offering 35% discount on all products). The application should allow the customer to select the product name from a list box.
Task List • Identify the data that needs to be displayed. • Identify the type of data binding. • Create a Web Form. • Write a code to bind data to controls. • Run the application.
Task 1: Identify the data that needs to be displayed. Result: • As per the given problem statement, the data to be displayed is as follows: • Product name • Product price • Discount amount • Discounted price
Task 2: Identify the type of data binding. • There are two types of data binding: • Single-value data binding – Involves binding controls, which can display a single data value at a time, to a data source. • Multi-record data binding – Involves binding server controls to classes, such as ArrayList and DataView, which implement the IEnumerable interface.
Task 2: Identify the type of data binding. (Contd.) • Single-value data binding can be in the following forms: • Binding to a property • Binding to a method • Binding to an expression
Task 2: Identify the type of data binding. (Contd.) • Multi-record data binding can be in the following forms: • Binding to array lists • Binding to a data view
Just a Minute… • What is the difference between multi-record controls and single-value controls? • Write the code to store any four cities in an array list and bind a DropDownList control to the values stored in the array list.
Task 2: Identify the type of data binding. (Contd.) Result: • The discounted price needs to be calculated based on the value entered by the user. The result needs to be displayed in a control. Therefore, you will use single-value data binding to bind and display the discounted price of a product. In addition, you will use multi-record data binding to bind an array list containing product names to a ListBox control.
Task 3: Create a Web Form. Task 4: Write a code to bind data to controls. Task 5: Run the application.
Web Server Control Templates • A template is a combination of HTML elements, controls, and embedded server controls and can be used to customize and manipulate the layout of a control. • Types of templates: • AlternatingItemTemplate • SelectedItemTemplate • HeaderTemplate • FooterTemplate • EditItemTemplate • SeperatorTemplate
Web Server Control Templates (Contd.) • PagerTemplate • ItemTemplate • Templates can be used to customize the layout of: • Repeater control • DataGrid control • DataList control
Just a Minute… • Write the code to create an ItemTemplate for a Repeater control and add a Button control to each row of the repeater control. • List the main differences between a Repeater control, a DataList control, and a DataGrid control.
Problem Statement 2.D.2 A Web application displaying product names and their prices in tabular format needs to be created. Each row containing product details should display a Know More button. When the customer clicks this button, the description for the selected product should be displayed.
Task List • Identify the data that needs to be displayed. • Identify the mechanism to bind data and display it in a customized layout. • Create a Web Form. • Write a code to bind data to controls and display the output. • Run the application.
Task 1: Identify the data that needs to be displayed. Result: • As per the given problem statement, the data to be displayed is as follows: • Product name • Product description • Product price
Task 2: Identify the mechanism to bind data and display it in a customized layout. Result: • As per the problem statement, data needs to be displayed and the user will not edit the data. Since, DataGrid and DataList controls are best suited for displaying editable data, you will bind the data to the Repeater control.
Task 3: Create the Web Form. Task 4: Write a code to bind data to controls and display the output. Task 5: Run the application.
Problem Statement 2.D.3 An online shopping application needs to be created that allows a customer to choose between different product categories and purchase a product from the desired category. After the customer has purchased a product, all orders placed by that customer for the current date should be displayed in a separate Web Form. The customer should be able to delete an order from the displayed order details. The application should provide enhanced performance and optimized solutions.
Task List • Identify the data that needs to be displayed. • Identify the mechanism to access and maintain data from a database. • Design the Web Forms. • Create the stored procedures. • Write a code to bind data from a database to controls and display the output. • Run the application.
Task 1: Identify the data that needs to be displayed. Result: • As per the given problem statement, the data to be displayed is as follows: • Product categories • Product details for each category • Customer ID • Product quantity • Credit card company • Credit card number • Order details
Task 2: Identify the mechanism to access and maintain data from a database. • ASP.NET allows you to create applications involving server-side data access by using ADO.NET. • Using ADO.NET components, you can perform the following on a database: • Retrieve data • Add data • Delete data • Edit data
Task 2: Identify the mechanism to access and maintain data from a database. (Contd.) Result: • As per the problem statement, the details of product categories need to be displayed to the user and order details need to be added in the database. Therefore, you will use ADO.NET to access and maintain the data in the database.
Task 3: Design the Web Forms. Task 4: Write a code to bind data from a database to controls and display the output. Task 5: Run the application.
Problem Statement 2.P.1 An online shopping application needs to be created where a customer can choose between different product categories and purchase a product from the desired category. After the customer has purchased the product, all the order details for the current date should be displayed in a separate Web Form. The customer should be able to delete or modify an order from the displayed order details. The application should provide enhanced performance and optimized solution.
Summary In this lesson, you learned that: • ASP.NET Web server controls can be bound to various data sources to display data stored in them. • The syntax for data binding is <%# expression %> where the portion of the code delimited with the characters<%# and %> is called data binding expression. • The DataBind() method evaluates the data binding expression and displays the evaluated expression in the server control’s bound property at runtime. • There are two types of binding data to server controls, single-value binding and multi-record binding.
Summary (Contd.) • Different types of data sources that can be bound to a single-value control are property, method, and expressions. • Multi-record server controls are the controls used to bind and display data from classes, such as ArrayList and DataView, which implement the IEnumerable interface. • A template is a combination of HTML elements, controls, and embedded server controls and can be used to customize and manipulate the layout of a control. • The different types of templates areAlternatingItemTemplate, SelectedItemTemplate, HeaderTemplate, FooterTemplate, EditItemTemplate, SeparatorTemplate, PagerTemplate, and ItemTemplate.
Summary (Contd.) • You can create and edit a template for a server control either by using Visual Designer or editing the ASP.NET code. • A DataGrid server control binds and displays data in a tabular format. You can customize the layout of a DataGrid control by using a template. A DataGrid control can be bound to any data source that supports the IEnumerable interface. • A DataList control displays data from a data source in the form of a list. The Items collection of the DataList control contains the data items in the control and is populated when the DataBind() method is called for the control.
Summary (Contd.) • A Repeater control displays data from different data sources by creating customized layouts. The Repeater control does not have a default layout. • ASP.NET allows you to create applications involving server-side data access by using ADO.NET.