1 / 15

Data Binding in ASP.NET

Data Binding in ASP.NET. What Is Data Binding?. Data binding is a solution to a problem that developers used to solve over and over in user-interface applications.

lynnea
Download Presentation

Data Binding in ASP.NET

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. Data Binding in ASP.NET

  2. What Is Data Binding? • Data binding is a solution to a problem that developers used to solve over and over in user-interface applications. • In the past, if you wanted to present data to a user, you had to retrieve the data from where it was stored and get it into your application. You then had to write custom code to render the data with graphics, or you could manually populate properties on controls with the data to display it. The way that you approached this would be different for each situation, each type of data, and each type of presentation that you provided. This required a lot of repetitive and error-prone code, which always cries out for a better solution.

  3. Easily data binding with .Net 2.0 • Nearly every dynamic web application performs some kind of data access, and fortunately ASP.NET 2.0 makes this incredibly easy to do. Unlike ASP.NET 1.0, which required developers to write custom code in order retrieve and bind data to server controls, ASP.NET 2.0 enables a declarative solution for data binding which requires no code at all for the most common data scenarios, such as: • Selecting and displaying data • Sorting, Paging and Caching Data • Updating, Inserting and Deleting Data • Filtering or Master-Details Using Parameters • ASP.NET 2.0 introduces two types of server controls that participate in this declarative data binding model. These two types of data controls handle the complexity of the stateless Web model for data scenarios, so developers don't need to understand page request lifecycle events just to perform data binding. Another benefit of this control-based model is that it can be easily extended to support additional data access storage providers.

  4. Data Source Controls • Data source controls are ASP.NET controls that manage the tasks of connecting to a data source and reading and writing data. Data source controls do not render any user interface, but instead act as an intermediary between a particular data store (such as a database, business object, or XML file) and other controls on the ASP.NET Web page. Data source controls enable rich capabilities for retrieving and modifying data, including querying, sorting, paging, filtering, updating, deleting, and inserting. ASP.NET includes the following data source controls:

  5. Data Source Controls • A data source control provides a single object in which you can declaratively define the following: • Connection information. • Query information, such as a SQL statement or the names of methods to invoke on an object. • Insert, update, and delete commands to modify data. • Parameters for queries, data modification, and filtering. • Options such as paging and caching (if the data source supports these options).

  6. Data-bound Controls • Many server controls in ASP.NET support data binding for populating their contents. These controls expose a DataSource property to be initialized to any object that supports the IEnumerable interface, as well as DataTables and DataSets. When the control's DataBind() method is invoked, the collection is traversed, and the contents of the control are filled with whatever was in the collection.Like the Render method of controls, the DataBind method invokes DataBind on any of the control's child controls. So invoking the Page's top-level DataBind() method implicitly invokes all the DataBind() methods for all controls on that page. Alternatively, you can elect to invoke each control's DataBind() method independently.

  7. Data-bound Controls • Data-bound controls are UI controls that render data as markup to the requesting client device or browser. A data-bound control can auto-bind to data exposed from a data source and will fetch data at the appropriate time in the page request lifecycle. These controls can optionally take advantage of data source capabilities such as sorting, paging, filtering, updating, deleting, and inserting. A data-bound control connects to a data source control through its DataSourceID property. You may be familiar with some of the data-bound controls in ASP.NET v1.x, such as DataGrid, DataList, Repeater, and list controls like DropDownList. ASP.NET 2.0 contains several new data-bound controls as well, such as:

  8. Data-Binding Syntax • Data-binding expressions are contained within <%# and %> delimiters and use the Eval and Bind functions. The Eval function is used to define one-way (read-only) binding. The Bind function is used for two-way (updatable) binding. In addition to calling Eval and Bindmethods to perform data binding in a data-binding expression, you can call any publicly scoped code within the <%# and %> delimiters to execute that code and return a value during page processing. • Data-binding expressions are resolved when the DataBind method of a control or of the Page class is called. For controls such as the GridView, DetailsView, and FormView controls, data-binding expressions are resolved automatically during the control's PreRender event and you are not required to call the DataBind method explicitly. • The following code example shows the use of data-binding expressions with a FormView control in an ItemTemplate. • <asp:FormView ID="FormView1" • DataSourceID="SqlDataSource1« • DataKeyNames="ProductID« • RunAt="server"> • <ItemTemplate> • <table> • <tr><td align=right><B>Product ID:</B></td> <td><%# Eval("ProductID") %></td></tr> • <tr><td align=right><B>Product Name:</B></td> <td><%# Eval("ProductName") %></td></tr> • <tr><td align=right><B>Category ID:</B></td> <td><%# Eval("CategoryID") %></td></tr> • <tr><td align=right><B>Quantity Per Unit:</B></td> <td><%# Eval("QuantityPerUnit") %></td></tr> • <tr><td align=right><B>Unit Price:</B></td> <td><%# Eval("UnitPrice") %></td></tr> • </table> • </ItemTemplate> • </asp:FormView>

  9. Using the Eval Method • The Eval method evaluates late-bound data expressions in the templates of data-bound controls such as the GridView, DetailsView, and FormView controls. At run time, the Eval method calls the Eval method of the DataBinder object, referencing the current data item of the naming container. The naming container is generally the smallest part of the data-bound control that contains a whole record, such as a row in a GridView control. You can therefore use the Eval method only for binding inside templates of a data-bound control. • The Eval method takes the name of a data field and returns a string containing the value of that field from the current record in the data source. You can supply an optional second parameter to specify a format for the returned string. The string format parameter uses the syntax defined for the Format method of the String class.

  10. Using the Bind Method • The Bind method has some similarities to the Eval method, but there are significant differences. Although you can retrieve the values of data-bound fields with the Bind method, as you can with the Eval method, the Bind method is also used when data can be modified. • In ASP.NET, data-bound controls such as the GridView, DetailsView, and FormView controls can automatically use the update, delete, and insert operations of a data source control. For example, if you have defined SQL Select, Insert, Delete, and Update statements for your data source control, using Bind in a GridView, DetailsView, or FormView control template enables the control to extract values from child controls in the template and pass them to the data source control. The data source control in turn performs the appropriate command for the database. For this reason, the Bind function is used inside the EditItemTemplate or InsertItemTemplate of a data-bound control. • The Bind method is typically used with input controls such as the TextBox control rendered by a GridView row in edit mode. When the data-bound control creates these input controls as part of its own rendering, it can extract the input values.

  11. Repeater Web Server Control Overview • The Repeater Web server control is a container control that allows you to create custom lists out of any data that is available to the page. The Repeater control does not have a built-in rendering of its own, which means that you must provide the layout for the Repeater control by creating templates. When the page runs, the Repeater control loops through the records in the data source and renders an item for each record. • Because the Repeater control has no default look, you can use it to create many kinds of lists, including the following: • A table layout • A comma-delimited list (for example, a, b, c, d, and so on) • An XML formatted list

  12. Using Templates with the Repeater Control • To use the Repeater control, you create templates that define the layout of the control's content. Templates can contain any combination of markup and controls. If no templates are defined, or if none of the templates contain elements, the control does not appear on the page when the application is run. • The following table describes the templates that are supported by the Repeater control.

  13. Binding Data to the Repeater Control • The Repeater control must be bound to a data source. The most common data source is a data source control, such as a SqlDataSource or ObjectDataSource control. Alternatively, you can bind a Repeater control to any class that implements the IEnumerable interface, which includes the ADO.NET datasets (DataSet class), data readers (SqlDataReader or OleDbDataReader classes), or most collections. • When binding data, you specify a data source for the Repeater control as a whole. When you add controls to the Repeater control — for example, when you add Label or TextBox controls in a template — you use data-binding syntax to bind the individual control to a field of the items returned by the data source.

  14. Events Supported bu the Repeater Control • The Repeater control supports several events. One of them, the ItemCreated event, gives you a way to customize the item-creation process at run time. The ItemDataBound event also gives you the ability to customize the Repeater control, but after the data is available for inspection. For example, if you were using the Repeater control to display a to-do list, you could display overdue items in red text, completed items in black text, and other tasks in green text. Either event can be used to override formatting from the template definition. • The ItemCommand event is raised in response to button clicks in individual items. This event is designed to allow you to embed a Button, LinkButton, or ImageButton Web server control in an item template and then be notified when the button is clicked. When a user clicks the button, the event is sent to the button's container — the Repeater control. The most common uses for the ItemCommand event are to program update and delete behaviors to the Repeater control. Since every button click raises the same ItemCommand event, you can determine which button was clicked by setting each button's CommandName property to a unique string value. The CommandSource property of the RepeaterCommandEventArgs parameter contains the CommandName property of the button that was clicked.

  15. For more information, please contact: Dzmitry_Chapko@epam.com http://www.epam.com

More Related