1 / 50

ASP.NET 2.0

ASP.NET 2.0. Chapter 8 Binding Data to Web Controls. Objectives. Designing a Web Data Application in the .NET Framework.

newton
Télécharger la présentation

ASP.NET 2.0

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASP.NET 2.0 Chapter 8 Binding Data to Web Controls

  2. Objectives ASP.NET 2.0, Third Edition

  3. Designing a Web Data Application in the .NET Framework • Today, business needs demand flexible database management systems. This flexibility has been achieved by means of the data access model, which provides a method whereby data can be shared across different applications and platforms. • Data source is a term that represents the data. The application data source can be a simple data structure, XML data, a file database such as Microsoft Access, or stored in a data server such as SQL Server or Oracle. The data access layer, also called the data tier or middle tier, interfaces with the data source. ASP.NET 2.0, Third Edition

  4. Designing a Web Data Application in the .NET Framework (continued) ASP.NET 2.0, Third Edition

  5. Data Access Components • The DataSet object can contain one or more DataTable objects. The DataTable object contains one or more rows of data with one or more columns. • A DataReader object is a read-only, forward-only stream of data from the database. A DataReader object is similar to the DataView object because it returns one or more rows of data. The DataReader object, however, is not a disconnected source of data. The data connection remains open while the DataReader object reads the data, row by row. The DataReader object provides better performance than the DataView or DataSet objects. If you use the DataReader object, you can read the rows only once. ASP.NET 2.0, Third Edition

  6. Creating DataSets and DataAdapters ASP.NET 2.0, Third Edition

  7. Creating DataSets and DataAdapters (continued) ASP.NET 2.0, Third Edition

  8. Creating DataSets and DataAdapters (continued) ASP.NET 2.0, Third Edition

  9. Creating DataSets and DataAdapters (continued) ASP.NET 2.0, Third Edition

  10. Creating DataSets and DataAdapters (continued) ASP.NET 2.0, Third Edition

  11. Creating DataSets and DataAdapters (continued) ASP.NET 2.0, Third Edition

  12. Creating DataSets and DataAdapters (continued) ASP.NET 2.0, Third Edition

  13. Creating DataSets and DataAdapters (continued) ASP.NET 2.0, Third Edition

  14. Data Binding Techniques ASP.NET 2.0, Third Edition

  15. Binding Techniques • The process of assigning the data source to a Web control is called data binding. The two types of data binding are single-expression binding and repeat-expression binding. • Web controls that are commonly used to display bound data are called Data controls. • Data binding allows you to get records synchronously, which means the ability to retrieve records is not tied to the page loading process. ASP.NET 2.0, Third Edition

  16. Binding Data to Web Controls ASP.NET 2.0, Third Edition

  17. Binding Data to Web Controls (continued) ASP.NET 2.0, Third Edition

  18. Repeated-Expression Binding • The CheckBoxList and RadioButtonList controls both display the data using the XHTML input tag with the type set to check box or radio. These controls are often used to represent Boolean data values in the database. • The DropDownList and ListBox controls both display the data using the XHTML select tag. The DropDownList control usually only displays one value at a time, however, while the ListBox control generally displays all the values at the same time. • The Repeater control is a small, lightweight control. The Repeater control displays the data using controls that you can define. No visible interface or formatting is available with the Repeater control; it simply repeats the content that you define. ASP.NET 2.0, Third Edition

  19. Repeated-Expression Binding (continued) • The DataList control displays the data as a basic list. You can modify the layout of the list by changing the number of horizontal or vertical columns. • The GridView control repeats content you define once for each data row. When the control is rendered, the page places the data in a table. • The DetailsView control displays content in a table like the GridView control, but only for one data row. It is often used to display information about a record that has been selected from a GridView control. • The FormsView control repeats content you define once for each data row. The FormsView control uses templates to render the content. The FormsView control is often used to create custom forms to insert, edit, and delete records. ASP.NET 2.0, Third Edition

  20. Binding Data to Web Controls and Data Controls • Data source is identified with the DataSource property of the DropDownList control; specify which item is displayed on the web page using the DataTextField property. • You use the DataValueField property to specify the value property of the DropDownList items • The DataValueField property corresponds to the value property of the HTML <option>control • The value is formatted using the DataTextFormatString property of the DropDownList control • The formats are entered using the standard format string rules, such as {0:c} for currency ASP.NET 2.0, Third Edition

  21. Data Binding Methods • DataBinder.Eval method is used to bind data one way, which means that the data can be read from the database • Bind method is used to provide two-way binding, which means that the data can be read from the database and the data will be uploaded to the database if the value changes • Container.DataItem naming container stores the results as the first argument ASP.NET 2.0, Third Edition

  22. Binding Data in a Collection to a Web Control ASP.NET 2.0, Third Edition

  23. Binding Data to a Data Control ASP.NET 2.0, Third Edition

  24. Binding Data to a Data Control (continued) ASP.NET 2.0, Third Edition

  25. Binding Data to a Data Control (continued) ASP.NET 2.0, Third Edition

  26. Automatic Binding Techniques • Automatic binding allows you to use the built-in wizards, and bind to data sources such as a Microsoft Access or SQL Server database, XML file, sitemap file, or business object • You do not have to call the DataBind method automatically ASP.NET 2.0, Third Edition

  27. Binding Data to a DataList Control • The DataList control allows you to display values as a simple list • The number of columns repeated is formatted using the RepeatColumns property; the RepeatColumns property must be an integer • The RepeatDirections property stores the direction of the columns ASP.NET 2.0, Third Edition

  28. Binding Data to a DataList Control(continued) ASP.NET 2.0, Third Edition

  29. Binding Data to a DataList Control (continued) ASP.NET 2.0, Third Edition

  30. Start 5/21/08 5:00 PM Segment ASP.NET 2.0, Third Edition

  31. Binding Relational Data to a GridView Control • The GridView control binds data to a table • The AutoGenerateColumns property does not allow you to specify the columns that you want to bind to your data source • (Note: You do not have to bind all of the data columns in your data source to the GridView control) • The HeaderText property allows you to change the string displayed at the top of the column headings • The DataFormatString property is used to display currency and is applied to the values displayed within the GridView control • The AllowSortingand AllowPaging properties allow you to configure the GridView control to support sorting and paging features ASP.NET 2.0, Third Edition

  32. Binding Relational Data to a GridView Control (continued) • Ch8_gridview.aspx • Instructions on 441: how to use the ‘smart tag’ to configure ASP.NET 2.0, Third Edition

  33. Amortization Table • Creating DataTable’s in program code • http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx • http://msdn.microsoft.com/en-us/library/system.data.datatable_members.aspx ASP.NET 2.0, Third Edition

  34. Overview of the ADO.NET Data Model ASP.NET 2.0, Third Edition

  35. ODBC and OLE DB • Open Database Connectivity (ODBC) was created to provide a common interface to relational database systems • ODBC drivers are used to provide access to a legacy ODBC-compliant database • Object Linking and Embedding Database (OLE DB) • OLE DB providers allow your application to access a database without an application- or database-specific interface ASP.NET 2.0, Third Edition

  36. .NET Providers • SQL Server .NET Provider is used to connect to an SQL Server database • OLE DB .NET Provider is used to connect to any other data source that can be accessed via the OLE DB interface • ODBC .NET Provider is used to access data using the ODBC interface • Oracle Provider is used to access an Oracle database ASP.NET 2.0, Third Edition

  37. .NET Providers (continued) ASP.NET 2.0, Third Edition

  38. .NET Providers (continued) • The Connection object provides the connection to the database; within the data connection, you must specify the location of the database, the access permissions, and any additional required parameters using a connection string • The Command object is used to identify an SQL command or a stored procedure; SQL is the Structured Query Language that is used to identify what records to add, delete, or modify within the database ASP.NET 2.0, Third Edition

  39. Retrieving Data in the .NET Framework • The DataReader object is used to deliver a stream of data from the database • When you call the ExecuteReader method of the Command object, the DataReader object opens a connection to the database and then retrieves rows of data as they are located in the database • The Read method of the DataReader object returns a single row and caches each row in memory only ASP.NET 2.0, Third Edition

  40. Using the DataReader to Retrieve Data from a Database ASP.NET 2.0, Third Edition

  41. Using The Data Reader • Web site chapter 8 • Ch8_datareader.aspx.vb • Open a connection • Set up a command with SQL • Create a reader object with the result • Iterate through the results and concatenate one column together • Populate a label with the result ASP.NET 2.0, Third Edition

  42. The DataAdapter and DataSet Objects • The DataAdapter object primarily is used to populate a DataSet object • The DataSet object is effectively a private copy of the database, and does not necessarily reflect the current state of the database • SelectCommand is used to retrieve data; when you use SelectCommand, the data must be inserted into the DataSet object • Fill method of the Data-Adapter object inserts the data returned from the SelectCommand into the DataSet object • InsertCommand – add a new record • UpdateCommand – modify the data within an existing record • DeleteCommand – remove a record from the database ASP.NET 2.0, Third Edition

  43. DataAdapter and DataSet ASP.NET 2.0, Third Edition

  44. DataSet and DataTables ASP.NET 2.0, Third Edition

  45. Using the DataSet to Retrieve Data from a Database • chapter8/ch8_dataset.aspx • Create a connection • Create a data adapter for the SQL command and the connection • Tell the adapter to fill a DataSet with the content of a named table • Bind the DataSet to a GridView control ASP.NET 2.0, Third Edition

  46. Using the DataSet Object to Retrieve Data from an XML File • Ch8_dataset_new.aspx • Look at structure of file • Code behind • Open file as a StreamReader • Populate a DataSet from the StreamReader • Bind the DataSet to a DataList • Web form • An item template picks out the fields of interest and formats those ASP.NET 2.0, Third Edition

  47. The DataView Object • A DataView gives a view of the data in a DataSet • A DataTable has a default view containing all its data • Ch8_dataview.aspx • Create connection, SQL, DataAdapter • Fill a DataSet • Create a DataView from the first table in the DataSet • Bind the DataView to a GridView • The GridView column templates pick out the appropriate data ASP.NET 2.0, Third Edition

  48. Summary • The Data Access Model provides a way to organize a data application logically. The data can be stored in various formats including XML, database files, and database servers. The data access layer provides the ability to connect to and manage the CRUD methods within the data source. • DataSet object is a disconnected set of data that is stored in memory. The DataSet object created by the DataAdapter object maintains the changes in the DataSet object, and pushes the changes to the server. • DataSet object consists of a DataTablesCollection and DataRelationsCollection. The DataTable object can consist of many DataView objects. • A DataView object is a subset of data from a single DataTable object. A DataSet object can be persisted as an XML document. You can import XML data into a DataSet object, or store the DataSet object as an XML document. ASP.NET 2.0, Third Edition

  49. Summary (continued) • A DataReader object is a read-only, forward-only stream of data from the database. The data connection remains open while the DataReader object reads the data. • Web controls use the DataSource property to identify the data to be bound. • The DataList control uses an ItemTemplate to display the values. When you use a template such as the ItemTemplate to display the data, use the DataItem property. • While the DataBinder.Eval method is used for one-way data binding, you need to use the Bind method for two-way data binding so the data can be read from the database and the data will be uploaded to the database if the value changes. • The GridView control allows you to create bound and unbound columns. Bound columns contain a value that is bound to a value in the database. ASP.NET 2.0, Third Edition

  50. Summary (continued) • The DataList and GridView controls use templates to create the header, footer, and item sections of the Data control. You can format these controls using the Auto Format templates. • Repeater control must be edited manually in HTML view. No graphical tool exists to edit the Repeater control. • In the ADO.NET model, you connect to data sources using Managed Providers. These providers maintain the interface with the lower-level providers and drivers that communicate directly with the data source. • You can write your application to interface directly with the ADO.NET framework, or use the built-in DataSource objects and Data controls. • The ADO.NET object model consists of four primary objects: Connection, Command, DataAdapter, and DataReader. Each of the .NET Providers contains its own version of these four ADO.NET objects. The DataAdapter object serves as a bridge between the DataSet object and the data source. The DataReader object is a high-performance object that provides a stream of data. The data is read-only and forward-only. ASP.NET 2.0, Third Edition

More Related