1 / 28

ASP.NET Data Binding

ASP.NET Data Binding. Lecture Overview. Understanding the ASP.NET data binding model. What is Data Binding?. We are, while the page executes, getting data into a page or control dynamically through an HTML page. Creating a Binding.

arwen
Download Presentation

ASP.NET Data Binding

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 Data Binding

  2. Lecture Overview • Understanding the ASP.NET data binding model

  3. What is Data Binding? • We are, while the page executes, getting data into a page or control dynamically through an HTML page

  4. Creating a Binding • In the ASPX page (not the C# file), we embed expressions in some “special” expressions

  5. Executing a Binding • Data binding has a place in the page lifecycle • It occurs automatically in some cases depending on the setting of control properties • In some cases you must force the binding to execute • Call DataBind() on the page or control • this.DataBind()

  6. Binding Example 2 • Bind to a form variable

  7. Binding Example 3 • Use data binding to call a function on the current page (The function is named SampleFunction)See DataBindingExpressions1.aspx • <asp:Label ID="Label2" runat="server" • Text="<%# SampleFunction() %>"></asp:Label>

  8. Possible Data Sources • Anything that can be enumerated (foreach loop) can serve as a data source • Collections for example • Dictionary and so on • ADO.NET DataSet and DataTable • We bind controls to data sources

  9. Binding (Simple) • We associate an ASP control to a single value • It can be just about anything • An executable expression • A variable • A data binding expression • This is what we did in the preceding example

  10. Binding (Repeated) • Repeated (list) • We bind to a list (hence repeating data) • Repeated data bind is used with “list” controls

  11. Types of Complex Bound Controls • Controls can be bound to a data source • Basically, repeating controls are of three types • List controls use a template for each item found in the data source • Iterative controls allow you to create a custom template for each row that is bound • View controls are the richest (DetailsView, FormView, GridView) • More later

  12. List Controls • These are our Listbox, DropDownList, CheckBoxList and RadioButtonList • They display a single field of information having multiple rows • The can be associated with a single-field hidden “key”

  13. Important Binding List Controls • DataSourceID: Set to an Access, SQL or other DataSource control • It’s a quoted string • DataSource: Set to a bindable source • DataValueField: Set to the data source field that you want the to behave as a key • DataTextField : Set to the data source field that you want the user to see • The DataKeyField contains the primary key

  14. Important Binding List Controls (Example)

  15. Binding List Controls • There are a host of list controls that all operate similarly • DropDownList, CheckBoxList, ListBox, RadioButtonList, etc.. • You can use the DataSource / DataMember technique or DataSourceID technique mentioned a moment ago

  16. ASP.NET Data Source Controls • They add a layer of abstraction to simplify your life • They create the OleDbDataAdapter, DataSet and DataTable for you • They take care of the binding details too

  17. ASP.NET Data Source Controls • SqlDataSource • AccessDataSource • ObjectDataSource • XmlDataSource • And more

  18. Data Binding (Using a Data Source - 1 ) • The DataSource and DataMember properties together bind a control instance to a data source (or DataSourceID) • The value varies based on the type of control • Bindable data sources are discussed in a moment • These properties can be set • Using the Properties window • Declaratively in .aspx files • Programmatically • Call DataBind to perform the binding

  19. Data Binding(Using a DataSource Control) • The DataSourceID property of a grid or other control contains the name of a data source control • Such as AccessDataSource, ObjectDataSource, SqlDataSource

  20. Binding List Controls (Example) • Programmatically bind a ListBox lstDemo.DataSourceID = AccessDataSource1"; lstDemo.DataTextField= "fldInvoiceNumber"; lstDemo.DataBind();

  21. List Controls (Members) • SelectedIndex contains the 0-based index of the selected list item • -1 if no item is selected • SelectedItem gets the selected item from the list • SelectedValue gets the value of the selected item • Set AutoPostBack to true to fire a postback when the user selects an item • We have discussed these previously

  22. Binding Order • Page.Init and Page.Load events fire • Other control events fire • Updates occur for changed controls (Inserting, Inserted, Updating, Updated events fire) • Page.PreRender fires • Data sources are queried and the data displayed

  23. Data-Binding Expressions(Introduction) • Data binding expressions bind a control property to data • Three ways to bind • Create a binding expression • Eval is used to create a one-way read-only binding • Bind is used to create a two-way updatable binding • Data binding expressions are evaluated when the DataBind method is called on the page or control

  24. Data-Binding Expressions (Syntax) • Data binding expressions are wrapped by <%# ... %> • Data binding expressions contain executable code • You must (often) explicitly call the DataBind method to force the data binding to happen

  25. About Eval and Bind • Eval and Bind must be used inside of a “bound container” such as a repeater or something like that!

  26. The DataList Control • It’s an iterative control • It supports selection and editing • Its contents can be displayed horizontally or vertically (RepeatDirection)

  27. The DataList Control • Its contents are manipulated using templates similar to the Repeater • ItemTemplate • AlternatingItemTemplate • SelectedItemTemplate • EditItemTemplate • HeaderTemplate • FooterTemplate • SeperatorTemplate

  28. Iterative Controls (Introduction) • They use templates to display each row differently • That is, you can customize the layout • Different templates exist for headers, footers, rows and alternating rows • The Repeater, DataList, and DataGrid controls are iterative controls • The Repeater has no predefined styles • The DataList and DataGrid do

More Related