ASP. NET 2.0 Data Binding Mechanism
To generate a new data binding control in ASP. NET 2.0, you must first determine which class is better suited to your requirements. However, your selection is not limited to null classes, such as Control, WebControl, or even ListControl. Let's explore the classes hidden behind the scenes. BaseDataBoundControl is the root of all data-bound control classes. It defines the DataSource and performanceid attributes and verifies the content they are allocated. DataSource accepts enumerated objects obtained and allocated in ASP. NET 1.x format.
- Mycontrol1.DataSource = dataSet;
- Mycontrol1.DataBind();
Performanceid is a string and the ID of the component bound to the data source. Once the control is bound to the data source, any further interaction between the two, whether read or write, will be out of your control and invisible. Both the good and the bad. Better, more specifically, it is great) The side is that it can eliminate a lot of code. The ASP. NET Framework ensures that the correct code is executed and the code is written according to the accepted best practices. You are more efficient, because you are completely confident that there will be no unpredictable errors during your work, so you can create pages faster. If you do not like this situation, it seems that many ASP. NET 1.x developers complain about this situation), you can continue to use the DataSource attribute and the DataBind method to program the old style. Moreover, in this case, the base class makes it unnecessary for you to do some common work, even if the effect is not so obvious in the code.
The DataBoundControl class is used for standard custom data binding controls that do not have much in common with existing controls. If you have to process your data set, manage view statuses and styles, and create a simple but customized user interface, this class provides a good starting point. The most interesting thing is that the DataBoundControl class connects the control to the data source component and hides any differences between the enumerated data source and the special component at the API level. In short, when you inherit from this class, you only need to override a method to receive data sets, whether the data source is a DataSet object or a newer data source component.
Let's elaborate on this, which represents a major change in the architecture ).
BaseDataBoundControl overrides the DataBind method originally defined on Control), and enables it to call the PerformSelect method. This method is marked as protected and abstract ). As its name implies, PerformSelect can retrieve valid data sets for binding. This method is protected because it contains implementation details; it is abstract and MustInherit in the Visual Basic Line), because its behavior can only be determined by the derived class such as DataBoundControl.
So what does DataBoundControl do to rewrite PerformSelect?
It connects to the data source object and obtains the default view. A data source object, such as a control such as SqlDataSource or ObjectDataSource, executes its selection command and returns the collection. The protected method for data retrieval is named GetData. It is smart enough to check the DataSource attribute. If DataSource is not empty, the bound object is packaged into a dynamically created data source view object and returned.
ASP. NET 2.0 Data Binding Mechanism: Next Step
The next step requires you to participate as a control developer. So far, the base class has completely automatically retrieved data from the ADO. NET object or data source component. The next step depends on what tasks you expect the control to complete. Here we can use the writable mdatabinding method. The following code snippet shows the implementation of this method in DataBoundControl. Note that the IEnumerable parameter passed by the Framework to this method only contains the data to be bound regardless of their source ).
- protected virtual void PerformDataBinding(IEnumerable data)
- {
- }
In the custom data binding control, you only need to override this method and fill in any control-specific set, such as the Items set that contains many list controls, such as CheckBoxList ). The display of the control's user interface occurs in the Render method or CreateChildControls, depending on the nature of the control. Render applies to list controls, while CreateChildControls is very suitable for composite controls.
One thing has not been explained yet: Who started the data binding process? In ASP. NET 1.x, data binding must explicitly call the DataBind method to start work. In ASP. NET 2.0, if you use the DataSource property to bind data to the control, you still need to do so. If you use the data source component through the performanceid attribute, avoid doing so. The data binding process is automatically triggered by the internal OnLoad event handler defined in DataBoundControl, as shown in the following pseudocode.
- protected override void OnLoad(EventArgs e)
- {
- this.ConnectToDataSourceView();
- if (!Page.IsPostBack)
- base.RequiresDataBinding = true;
- base.OnLoad(e);
- }
Data is retrieved and bound whenever the control is returned or loaded to the page for the first time. The data source determines whether to run the query again or use some cached data.
If this page is displayed for the first time, the RequiresDataBinding attribute is also enabled to bind data. When the assigned value is true, DataBind is called internally by the setting program of this attribute. The pseudocode below shows the internal implementation of the RequiresDataBinding setup program.
- protected void set_RequiresDataBinding(bool value)
- {
- if (value && (DataSourceID.Length > 0))
- DataBind();
- else
- _requiresDataBinding = value;
- }
As you can see, for backward compatibility, DataBind is automatically called only when the performanceid is not empty or you are bound to the ASP. NET 2.0 data source control. In this case, if you explicitly call DataBind, it will lead to dual data binding.
Note that DataSource and performanceid cannot be set at the same time. In this case, an invalid operation exception is thrown.
Finally, I will mention the protected method EnsureDataBound. This method is defined on the BaseDataBoundControl class, which ensures that the control has been correctly bound to the required data. If RequiresDataBinding is true, this method calls DataBind, as shown in the following code snippet.
- protected void EnsureDataBound()
- {
- if (RequiresDataBinding && (DataSourceID.Length > 0))
- DataBind();
- }
If you have compiled complex and complete data binding controls, you may already know what I mean. In ASP. in NET 1.x, the data binding control is usually designed to generate its own user interface in the following two cases: the control has full access to the data source, or the control is based on the view status. When the control needs to manage its own send-back events, for example, assuming that the control supports pagination DataGrid, the two options mentioned above seem to be two extreme cases. In ASP. NET 1.x, these controls are the same. Consider DataGrid. There is only one solution: to raise an event to the home page to be refreshed. This method causes the well-known problem of redundant code in ASP. NET 1.x pages-this is also the problem of calling the data source component to fix.
In ASP. NET 2.0, you must set RequiresDataBinding to true whenever data binding is required during the control's lifecycle. Setting this property will trigger the corresponding data binding mechanism and re-create the updated version of the internal infrastructure of the control. The built-in OnLoad event handler also connects the control to the data source. To be effective, this technology must rely on Smart Data source controls that cache their data in a certain location. For example, the SqlDataSource control supports many attributes so that any bound result set can be stored in the ASP. NET cache within a given period of time.
The above introduces ASP. NET 2.0 Data Binding Mechanism: how to generate a new data binding control.
- Brief Introduction to the Development of ASP. NET 2.0 Data Binding
- Overview ASP. NET Excel Process calling
- Analysis of Theme functions in ASP. NET development skills
- ASP. NET Dynamic Compilation
- Analysis on ASP. NET supported by Apache