+ All Categories
Home > Documents > Ch 7 data binding

Ch 7 data binding

Date post: 15-Jan-2015
Category:
Upload: madhuri-kavade
View: 1,225 times
Download: 2 times
Share this document with a friend
Description:
 
Popular Tags:
25
DATA BINDING
Transcript
Page 1: Ch 7 data binding

DATA BINDING

Page 2: Ch 7 data binding

2

Almost every web application has to deal with data, whether it’s stored in a database, an XML file, a structured file, or something else.

Retrieving this data is only part of the challenge—a modern application also needs a convenient, flexible, and attractive way to display the data in a web page.

ASP.NET includes a rich and full-featured model for data binding.

Data binding allows you to bind the data objects you’ve retrieved to one or more web controls, which will then show the data automatically.

Don’t need to write time-consuming logic to loop through rows, read multiple fields, and manipulate individual controls.

INTRODUCTION TO DATA BINDING

Page 3: Ch 7 data binding

3

To make your life even easier, use ASP.NET’s data source controls.

A data source control allows you to define a declarative link between your page and a data source.

Configure a data source control, and hook it up to your web controls at design time, and ASP.NET will take care of all the data binding details.

Introduction to Data Binding cont..

Page 4: Ch 7 data binding

4

Data binding is a feature that allows you to associate a data source with a control and have that control automatically display your data.

Key characteristic is that it’s declarative.

Most web controls (including TextBox, LinkButton, Image, and many more) support single-value data binding.

To use single-value binding, create data binding expressions.

Many web controls support repeated-value binding.

Assign DataSource property of the control to data object and call DataBind() method.

Basic Data Binding

Page 5: Ch 7 data binding

5

The controls that support single-value data binding allow you to bind some of their properties to a data binding expression. The expression is entered in the .aspx markup portion of the page, not in code behind file. Here’s an example:

<%# expression_goes_here %> For example, if you have variable in your page class named EmployeeName, you could write the following: <%# EmployeeName %> To evaluate a data binding expression such as this, call the Page.DataBind() method. The following data binding expressions are all valid:

<%# GetUserName() %><%# "John " + "Smith" %><%# Request.Browser.Browser %>

Single-Value Binding

Page 6: Ch 7 data binding

6

ASP.NET also has support for a different type of expression, commonly known as $ expressions.

$ expression is a code sequence in an .aspx page and will be evaluated by an expression builder when the page is rendered.

ASP.NET includes a built-in expression builder that allows to extract custom application settings and connection string information from the web.config file.

For example, if you want to retrieve an application setting named appName from the <appSettings>

<asp:Literal Runat="server" Text="<%$ AppSettings:appName %>" />

Here’s an example that uses the ConnectionStringsExpressionBuilder:

<asp:Literal Runat="server" Text="<%$ ConnectionStrings:Northwind %>" />

Other Types of Expressions

Page 7: Ch 7 data binding

7

Repeated-value binding allows you to bind an entire list of information to a control. Could be a collection of custom objects (for example, in an ordinary ArrayList or Hashtable) or a collection of rows (for example, with a DataReader or DataSet). Important properties of repeated controls: DataSource DataSourceID DataTextField DataTextFormatString

Repeated-Value Binding

Page 8: Ch 7 data binding

8

Can bind any data structure that implements the ICollection interface or one of its derivatives.

The following list summarizes many of these data classes

All in-memory collection classes, such as Collection, ArrayList, Hashtable, and Dictionary.

An ADO.NET DataReader object, which provides connection-based, forward-only, and read-only access to the database

The ADO.NET DataView, which provides a view onto a single disconnected DataTable object.

Any other custom object that implements the ICollection interface.

Binding to a DataReader

Page 9: Ch 7 data binding

9

In addition to the simple list controls, ASP.NET includes some rich data controls that support repeated-value binding. Rich Controls are designed exclusively for data binding. Support higher-level features such as editing and other features. The rich data controls include the following:

GridView : The GridView is an all-purpose grid control for showing large tables of information

DetailsView: Shows a single record at a time, in a table that has one row per field

FormView: FormView shows a single record at a time, supports editing, and provides paging controls

The Rich Data Controls

Page 10: Ch 7 data binding

10

GridView provides a DataSource property for the data object. DataBind() method triggers to read the data object and display each record. GridView automatically generates a column for every property or every field. For Example:

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="true" />Now, define a query that selects several fields from the Employees

string sql = "SELECT EmployeeID, FirstName, LastName, Title, City " +

"FROM Employees"; Bind the GridView to a DataReader . grid.DataSource = reader;

grid.DataBind();

The Rich Data Controls cont..

Page 11: Ch 7 data binding

11

To access database data access code is needed. With data source controls, you can avoid writing any data access code. The data source controls include any control that implements the IDataSource interface. The .NET Framework includes the following data source controls:

Data Source Control Description

SqlDataSource Allows you to connect to any data source that has an ADO.NET data provider. Includes SQL Server, Oracle, and the OLE DB or ODBC data sources.

ObjectDataSource Allows you to connect to a custom data access class.

AccessDataSource Allows you to read and write the data in an Access database file (.mdb).

XmlDataSource allows you to connect to an XML file

SiteMapDataSource allows you to connect to the Web.sitemap file thatdescribes the navigational structure of your website

Data Source Controls

Page 12: Ch 7 data binding

12

Data source controls turn up in the .aspx markup portion of your web page like ordinary controls. Here’s an example:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ... /> The SqlDataSource represents a database connection that uses an ADO.NET provider..NET ships with these four provider factories System.Data.SqlClient System.Data.OracleClient System.Data.OleDb System.Data.Odbc

The SqlDataSource

Page 13: Ch 7 data binding

13

The next step is to supply the required connection string. For example

<configuration><connectionStrings><add name="Northwind“

connectionString="Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI"/>

</connectionStrings>...</configuration>

Specify it in the SqlDataSource using a $ expression.

<asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:Northwind

%>" ... />

The SqlDataSource cont..

Page 14: Ch 7 data binding

14

Each SqlDataSource control allows you to retrieve a single query. Can add corresponding commands for deleting, inserting, and updating rows. The SqlDataSource command logic is supplied through four properties:

SelectCommand, InsertCommand, UpdateCommand, and DeleteCommand

Example:

<asp:SqlDataSource ID="sourceEmployees" runat="server"

ProviderName="System.Data.SqlClient"

ConnectionString="<%$ ConnectionStrings:Northwind %>" SelectCommand="SELECT EmployeeID, FirstName, LastName, Title, City FROM Employees"/>

Selecting Records

Page 15: Ch 7 data binding

15

Clearly, the great advantage of the data source controls is that they allow you to configure data binding at design time, without writing tedious code.

Controls can be bound to a DataReader or a DataView

Which approach does the SqlDataSource control use?

DataSourceMode to SqlDataSourceMode.DataSet (the default) or to SqlDataSourceMode.DataReader.

The DataSet mode is almost always better, because it supports advanced sorting, filtering, and caching settings.

Selecting Records cont..

Page 16: Ch 7 data binding

16

Can we pass parameters to SqlDataSourceMode???

<asp:SqlDataSource ID="sourceEmployees" runat="server"

ProviderName="System.Data.SqlClient"ConnectionString="<%$

ConnectionStrings:Northwind %>"SelectCommand="SELECT EmployeeID,

FirstName, LastName,Title, City FROM Employees WHERE City=@City">

<SelectParameters><asp:ControlParameter ControlID="lstCities"

Name="City"PropertyName="SelectedValue" /></SelectParameters>

</asp:SqlDataSource>

Parameterized Commands

Page 17: Ch 7 data binding

17

Can we use stored procedure with SqlDataSource control???

<asp:SqlDataSource ID="sourceEmployees" runat="server"

ProviderName="System.Data.SqlClient"ConnectionString="<%$

ConnectionStrings:Northwind %>"SelectCommand="GetEmployeesByCity"

SelectCommandType="StoredProcedure"><SelectParameters><asp:ControlParameter ControlID="lstCities"

Name="City"PropertyName="SelectedValue" /></SelectParameters>

</asp:SqlDataSource>

Stored Procedures

Page 18: Ch 7 data binding

18

Parameter values aren’t necessarily drawn from other controls. Can map a parameter to any of the parameter types defined in table belowSource Control Tag Description

Control property

<asp:ControlParameter>

A property from another control onthe page.

Query string value

<asp:QueryStringParameter>

A value from the current query string.

Session state value

<asp:SessionParameter>

A value stored in the current user’ssession.

Cookie value <asp:CookieParameter>

A value from any cookie attached to thecurrent request.

Profile value <asp:ProfileParameter> A value from the current user’s profile

A form variable

<asp:FormParameter> A value posted to the page from aninput control.

Set programmatically

<asp:Parameter> Use code to set a parameter value by hand.

More Parameter Types

Page 19: Ch 7 data binding

19

Don’t need to remember the different tag names Visual Studio provides a handy editor that lets you create your command and define your parameters.

More Parameter Types cont..

Page 20: Ch 7 data binding

20

ASP.NET’s rich data controls—including the GridView, DetailsView, and FormView—all have editing features First step is to define suitable commands for the operations (like InsertCommand, DeleteCommand, and UpdateCommand) Example<asp:SqlDataSource ID="sourceEmployees" runat="server"

ProviderName="System.Data.SqlClient"ConnectionString="<%$

ConnectionStrings:Northwind %>"SelectCommand="SELECT EmployeeID,

FirstName, LastName, Title, City FROM Employees"UpdateCommand="UPDATE Employees SET

FirstName=@FirstName, LastName=@LastName,Title=@Title, City=@City FROM Employees WHERE EmployeeID=@EmployeeID"></asp:SqlDataSource>

To enable editing, set the GridView.AutoGenerateEditButton property to true.

Updating Records

Page 21: Ch 7 data binding

21

Can use stored procedures to update the records

Example <asp:SqlDataSource ID="sourceEmployees" runat="server"

ProviderName="System.Data.SqlClient"ConnectionString="<%$

ConnectionStrings:Northwind %>" SelectCommand="SELECT EmployeeID, FirstName, LastName, TitleOfCourtesy FROM Employees"

UpdateCommand="UpdateEmployee" UpdateCommandType="StoredProcedure"

OnUpdating="sourceEmployees_Updating" ><UpdateParameters>

<asp:Parameter Name="First" Type="String" />

<asp:Parameter Name="Last" Type="String" />

</UpdateParameters></asp:SqlDataSource>

Updating Records

Page 22: Ch 7 data binding

22

Deleting a record is similar to updating it. Create a DeleteCommand that removes the record you want to delete. Example

<asp:SqlDataSource ID="sourceEmployees" runat="server"

ProviderName="System.Data.SqlClient"ConnectionString="<%$

ConnectionStrings:Northwind %>"SelectCommand="SELECT EmployeeID,

FirstName, LastName, Title, City FROM Employees"

DeleteCommand="DELETE Employees WHERE EmployeeID=@EmployeeID "></asp:SqlDataSource>

Deleting Records

Page 23: Ch 7 data binding

23

The GridView supports editing and deleting records, but it doesn’t support insertion. However, the DetailsView and FormView do support insertion. Example

<asp:SqlDataSource ID="sourceEmployees" runat="server"

ProviderName="System.Data.SqlClient"ConnectionString="<%$

ConnectionStrings:Northwind %>"SelectCommand="SELECT EmployeeID, FirstName, LastName,

Title, City FROM Employees”InsertCommand="INSERT INTO Employees (FirstName,LastName,Title,City)VALUES

(@FirstName,@LastName,@Title,@City)"></asp:SqlDataSource>

Inserting Records

Page 24: Ch 7 data binding

24

Data access logic embedded in the page. Maintenance in large applications. Lack of flexibility. Inapplicability to other data tasks.

Disadvantages of the SqlDataSource

Page 25: Ch 7 data binding

Thank you!!!


Recommended