Introduction
Recently, when I was working on the Truelove project, I used the gridview + objectdatasource + nbear connector to get the data and bind it to a piece. It was amazing to display it on the page as needed. I always want to see what happened under this transaction. How the data is bound together, what mechanism is used, and a series of question marks. I have referred to some of the msdn and other websitesArticleFinally, I figured out the real process and did not dare to exclusively share it with you.
Base Class basedatabound
All data binding controls bound to data using the new data source control are inherited from the basedatabound class. This class has the following two public attributes.
Public attribute of basedatabound
Virtual ObjectDatasource {Get;Set;}Virtual StringPerformanceid {Get;Set;}
There are also two template methods:
template methods
protected abstract void effecmselect (); protected abstract void validatedatasource ( Object datasource);
The first attribute datasource was bound to data in the Asp.net 1.1 era. Second, we used to bind performancecontrol in the Asp.net 2.0 era. If the datasource attribute is set, the validatedatasource method will be called to check whether the bound data is usable (usually to determine whether it is ilistsource, ienumerable or idatasource ).
If you change the performanceid attribute after the initialization phase, the databind function can be called only when the prerender is used. If you change it after the prerender, The databind function will be called immediately. Note that if you set the performanceid value before preload, The databind function will not be called.
The following is the section of the life cycle of the basedatabound class on the entire Asp.net page:
- Oninit: If viewstate is unavailable, control will call it at each prerender.
- Onpreload: sets an internal flag to identify the end Of the init phase.
- Onprerender: Set an internal identifier to start with prerender and call databind.
The basedatabound class has a public mothed
Public mothed
Public Override VoidDatabind ();
This method calls the performselect template method. The performselect method obtains data from the datasource below.
Control databoundcontrol
Databoundcontrol is inherited from the accumulation of basedatabound. databoundcontrol is the base class of all data-bound controls. In this analogy, basedatabound has one more public attribute:
Public property
Public Virtual StringDatamember {Get;Set;}
This attribute is used when the data source exports more than one data column.
The main task of this class is to complete the separation of classes derived from the data extracted from the data source. The data obtained from the data source is considered as a set of derived classes that implement the ienumerable interface in the template method mongomdatabinding.
Protected internal virtual mothed
Protected Internal Virtual VoidExtends mdatabinding (ienumerable data );
Fortunately, the most difficult work was completed by the connecttodatasourceview method. This method can obtain a reference to performanceview. This is a great improvement for controls using performanceid. For datasource, you need to use an embedded class to package datasource into a performancecontrol (readonlydatasource implements the idatasouce interface, returns a view object (readonlyperformanceview) that only supports the select method ))
The position of the entire databoundcontrol in the life circle of the Asp.net page is as follows:
- Onpreload: If this page is requested to call the onpreload method of the base class for the first time, if viewstate is available but control is not bound to data, set a flag to try to bind data in the onprerender phase.
- Onload: If there is no view of the data source, we try to get it, and in the following situation: If for some reason, we didn't try to bind data in onpreload and there is still no data bound, set an identifier so that it tries to bind data when onprerender.
We can see that this class solves the problem that the performanceid attribute cannot be automatically bound in the pre-load phase left by its base class.
If the control uses the performanceid attribute to bind data, the control will capture the performanceviewchanged event activated by the performanceview due to the re-binding of the control when the data source changes (I will explain it in the performanceview phase in subsequent articles ).
This class implements the template Methods validatedatasource and performselect defined in the base class. First, check whether the data source implements one of the ilistsource, ienumerable, or idatasource interfaces. If not, an exception is thrown. Then, the performselect called by the databind method completes the following:
- If the control binding is based on the datasource attribute, the databinding event is activated.
- Obtain performanceview from the data source
- Bind control to data
- Construct the select parameter, call the select method of performanceview, and pass in a callback method that will be called after the select method is executed.
This method will be called after the select method is executed. The performdatabinding method is called in this method. If you still remember that this method is the method that passes in the data that is called by ienumerable.
I will introduce performancecontrol and performanceview in subsequent articles. This article mainly refers to Manuel Abadia's article, which involves reading and translating while experiencing the taste of writing. You may be confused. I will explain it in subsequent articles and draw a link call diagram. I am looking for a good drawing tool. I wonder if you have recommended it.
My wife has a cold and is uncomfortable today. I hope she can recover soon. My wife's sister will go to the house tomorrow and call the prepayment. I don't know when we can buy a house. I look forward to ing...