Data Binding with ASP.NET

ASP.NET provides a rich set of controls that enable you to display information to users as well as accept information from users. You can display information in controls from a wide variety of data stores, such as properties, arrays, data structures, or databases.

Some of the data stores are static, whereas others are dynamic. You usually use static data stores to display information for user reference. In addition to displaying static information in controls, there are situations that require you to display information dynamically.

For example, you might need to display the discount based on the quantity purchased for a product. Also, you might need a control to display information from a database whose data keeps changing constantly. In such situations, ASP.NET provides a solution by providing a feature that allows data binding to controls.

Data binding means binding controls to information stored in a data store. Here, the term "data" is used in a very broad sense. When we talk about data binding, it implies binding any control property to almost any kind of data store. A data store can be as simple as a public property on a page, or as complex as a database stored on a server. This broad choice among data stores provides high flexibility, and thus enables you to bind a control to any data store based on your need.

The Web Forms controls that are bound to a data store access data through the properties of specific classes, categorized as data classes. Data classes typically include methods that can be used for updating the underlying data stores. Because the term "data" is used in a broad sense, the class category "data classes" is also used in a generic, broad sense. These classes differ depending on the data store. Some data classes provide more functionality than others — you can use any one of these classes depending on your need.

You can bind a control to different data stores, such as properties, methods, or collections. These different data stores can be bound to a control property by using data binding expressions. While binding, the data is always bound to a particular property of the control (the property name might differ for various controls). When a data binding expression is evaluated, the resulting value is loaded in the control's bound property.

You can bind simple controls to public properties. A public property can be of a control on a page or the page itself.

Simple controls are the controls that can bind only to a single value. Some simple controls include Label, TextBox, and Button controls.

You can bind complex controls to any structure by using a data class that implements the ICollection interface, which ensures that the data classes provide the basic functionality of data access and navigation. For example, you can bind a DropDownList control to an array by using the ArrayList class, which implements the ICollection interface.

Complex controls are the controls that contain embedded controls. Some complex controls include DataList, Repeater, and DataGrid controls.

When you bind a control property to a data store, the Web Forms Framework cannot evaluate data binding expressions automatically. To display the evaluated value in the control's bound property, you need to call the DataBind() method explicitly. The page and each control on the page support this method. When you call the DataBind() method for a control, it is cascaded to all its child controls. For example, if you call the DataBind() method for the page control, the method is automatically called for all the controls on the page.

The explicit method for data binding enables you to control when to load the bound controls with data. Therefore, calling the DataBind() method explicitly should not be seen as a disadvantage. You can load the bound controls with data in one of the following situations:

When you need to display data in the bound controls as soon as the page is loaded. In such a situation, you can call the DataBind() method at the Page_Load stage. You can call the DataBind() method for the page or for a specific control, depending on your requirement.

When data in the dataset is updatd and you want to display the updated data in the bound controls. In such a situation, you need to call the DataBind() method in the event -handling methods that resulted in the change to the dataset. Again, you can call the DataBind() method for the page or for specific controls depending on whether you want to refresh the complete
page or specific controls.

Data Source Binding

You can use the ASP.NET syntax to bind data to the ASP.NET server controls. To bind these controls to data, you first need to open the ASPX file for the Web Forms page. Then, you can use the appropriate syntax for data binding for different controls. To bind a property of a control to some data represented by an expression, use the following tag:

<% # expression %>

When the expression is evaluated, the value is loaded in the control's bound property.

For example, you can bind the Text property of a Label control to a public property called ProductID of the page.

In this code, the <% # ProductID %> tag is used to bind the Text property of the Label control to the ProductID property of the page.

You should be careful while using quotation marks in the expression. If the expression contains quoted strings, you should use single quotation marks.
As mentioned earlier , the control property will not display the bound data until the DataBind() method is called explicitly. You can call the DataBind() method for the page by using the following statement:

Page.DataBind()

Binding methods

Binding methods is similar to binding any other expression. In this section, you'll create a method to return a string based on the value selected from the DropDownList control with ID CustState. Then, you'll bind the Text property of the Display Label to this method. To implement this binding, write the following code in the tag to create a method called StateValue:

Function StateValue() As String

If CustState.SelectedItem.Text="None" Then
StateValue="Not a US resident"
Else
StateValue="Selected state: " + CustState.selectedItem.Text
End If
End Function

The StateValue method returns a string, "Not a US resident", if a user selects None from the DropDownList control with ID CustState. If the user selects any other value, the method returns the selected state. Next, you need to bind this method to the Text property of the Label control with ID DisplayLabel.

Binding to a DataView

The DataView class represents a custom view of a data table. This class is a member of the System.Data namespace, and to use this class in your page, you need to import the System.Data namespace. Before you can implement binding to a data table, add a new Web Forms page to your ASP.NET Web Application project. In this form, add a DataGrid control. Then, to implement the functionality of the DataView class in this new form, you'll need to import the System.Data namespace. To do so, write the following statement in the ASPX file of the new Web Forms page:

<%@import Namespace="System.Data"%>

You can bind a DataView object to a DataGrid control. A DataGrid control displays information in row and column format. In this section, you'll create an object of the DataView class. This object represents a data table that displays cities and their respective states. Then, you'll bind this DataView object to the DataSource property of the DataGrid control.

RELATED POST

CREATING ASSEMBLIES WITH ASP.NET

BUILDING HANDLERS IN ASP.NET

INTERFACES AND CLASSES CREATION IN ASP.NET

CACHING IN ASP.NET

CACHING IN ASP.NET PART TWO

WIRE LESS APPLICATION WITH ASP.NET

SECURITY IN ASP.NET PART ONE

SECURITY IN ASP.NET PART TWO

LOCALIZING ASP.NET APPLICATIONS

DEPLOYING ASP.NET APPLICATIONS

ASP.NET CONFIGURATION SYSTEM

WEB SERVICES IN ASP.NET

WEB SERVICES PART TWO

WEB SERVICE INFRASTRUCTURE

WEB SERVICE INFRASTRUCTURE PART TWO

EXCHANGING MESSAGES IN ASP.NET

MICROSOFT HAIL STORM

SOAP AND DOT NET PART ONE

SOAP AND DOT NET PART TWO


No comments:

Post a Comment