Data Binding Overview


In this section, we'll provide an overview of the data-binding architecture in the page framework. We'll assume that you have used data-binding expressions ( <%# %> ) in .aspx pages and have used the built-in ASP.NET data-bound controls. We'll first examine how the page supports data-binding expressions and then look at the key concepts of implementing your own data-bound control.

You do not have to perform any additional work in your control to support data-binding expressions. That functionality is implemented in the base Control class and is applicable to any read/write property of your control. This is how data-binding expressions work: When the page parser sees a data-binding expression within your control's tags in an .aspx page, the parser generates event handlers that evaluate the data-binding expressions and perform property assignments. The page parser wires up those event handlers to your control's DataBinding event, which your control inherits from the Control class. In Chapter 2, "Page Programming Model," we described the dynamic compilation model and how the parser generates code from declarative page syntax. In Chapter 9, "Control Life Cycle, Events, and Postback," we described the intrinsic events of the Control class, and in Chapter 3, "Component Programming Overview," we defined the .NET Framework event architecture.

As we explained in Chapter 3, each event has a protected On< EventName > method that raises the event by invoking the event handlers that are associated with it. The Control class defines the protected OnDataBinding method that raises the DataBinding event. Control also exposes the public Data ­Bind method whose default implementation invokes the OnDataBinding method. Thus, when a page developer invokes the DataBind method of your control, this method in turn raises the DataBinding event of your control, which causes the event handlers that are wired to it to be executed. You can see the event handlers that the parser generates and how they are wired to the Data ­Binding event of your control in the class that the parser generates from the .aspx page by using the technique we described in Chapter 2.

Data binding occurs when the page developer invokes the DataBind method of a control. The DataBind method of a control recursively invokes the DataBind method on each of its child controls in the control tree.

ASP.NET has an explicit data-binding model. Under this model, the page developer invokes data-binding logic only when needed, by calling the Data ­Bind method. This model offers Web applications a significant gain in performance over the implicit data-binding model in which the data-binding logic is automatically triggered. In a disconnected Web environment, it is not efficient to connect to the data source upon each request because the data binding process might involve creating database connections or other resources and executing time-consuming queries. In general, the page performs data-binding logic on the first request, and controls restore their state on subsequent requests by using the saved view state. If the data changes subsequently, the page developer can rebind either the entire page or a selective group of controls.

Now let's examine the main characteristics of the standard ASP.NET data-bound controls from the perspective of developing a custom data-bound control. The standard data-bound controls expose a DataSource property that allows the page developer to specify the data source to bind to. These controls perform data-binding logic when their DataBind method is invoked. In addition, these controls restore their state on postback when the page developer does not invoke the DataBind method.

Here are the main steps that you must implement to create a data-bound control:

  • Expose a DataSource property that allows the page developer to specify a source of data to bind to.

  • Override the DataBind method to implement the logic to enumerate the objects in the assigned data source and to create a child control hierarchy that visually represents the data source. From the Data ­Bind method, you must first invoke the OnDataBinding method of your control to raise the DataBinding event. This causes any data-binding expressions that a page developer might have associated with your control to be evaluated, as we explained earlier in this section.

  • Override the CreateChildControls method to create the child control hierarchy by using the view state saved at the end of the previous request. The CreateChildControls method is called on postback if the page does not invoke DataBind on your control. As we described in Chapter 12, the default implementation of the OnPreRender method invokes CreateChildControls unless child controls have been created before the PreRender phase.

We'll look at these steps in greater detail when we implement the DataBoundTable control in the next section.

BindableAttribute and the DataBindings Property

Microsoft Visual Studio .NET provides a UI (accessed from the property browser) that allows a page developer to easily associate data-binding expressions with a control. The DataBindings property in the property browser is a design-time-only property, which is implemented by the ControlDesigner base class. This property is associated with a UI type editor that allows page developers to edit data-binding expressions for all the properties of a control marked with Bindable(true) . The Control and WebControl classes use Bindable(true) to mark properties that are meaningful to bind data to. You should do the same for any properties to which you want to enable the page developer to bind data.

Later in this chapter, in the "Implementing a Data-Bound Control Designer" section, we'll show you how implement a designer for a data-bound control that provides many design-time features that greatly enhance the control's usability at design time.



Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net