Docsity

ASP.NET Data Binding: Understanding and Implementing Dynamic Data in ASP.NET Pages, Slides of Computer Programming

Partial preview of the text

Download ASP.NET Data Binding: Understanding and Implementing Dynamic Data in ASP.NET Pages and more Slides Computer Programming in PDF only on Docsity!

ASP.NET Data Binding

Lecture Overview

 Understanding the ASP.NET data binding model

Creating a Binding

 In the ASPX page (not the C# file), we embed expressions in some “special” expressions

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()

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>

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

Binding (Repeated)

 Repeated (list)  We bind to a list (hence repeating data)  Repeated data bind is used with “list” controls

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

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 docsity.com

Important Binding List

Controls (Example)

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

ASP.NET Data Source Controls

 SqlDataSource  AccessDataSource  ObjectDataSource  XmlDataSource

 And more

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

Binding List Controls

(Example)

 Programmatically bind a ListBox

lstDemo.DataSourceID = AccessDataSource1"; lstDemo.DataTextField = "fldInvoiceNumber"; lstDemo.DataBind();