This series of articles will introduce the use of ObjectDataSource, in order to complete the content, so although simple, but still sent to the home page, do not know can not
This series of articles mainly refer to msdn,asp. NET QuickStart and asp.net of the article, will explain the use of ObjectDataSource, only for reference, do not guarantee content 100% of the fight
1 Comparison of SqlDataSource and ObjectDataSource controls
ASP.NET2.0 provides a SqlDataSource data source control that supports the properties used to specify a connection string, SQL statement, or stored procedure to query or modify the database. However, there is a problem with the SqlDataSource control: The disadvantage of the control is that it forces you to mix the user interface layer with the business logic layer. However, as the size of the application expands, you will increasingly feel that mixing multiple layers is undesirable. When building a strictly multi-tiered WEB application, you should have a clear user interface layer, business logic layer, and data access layer. It is not advisable to refer to SQL statements or stored procedures at the user interface level simply because of the coercion of the SqlDataSource control.
The choice of SqlDataSource and ObjectDataSource, in the sense that the former is suitable for most small personal or amateur sites, and for larger enterprise applications, storing SQL statements directly in the rendering page of an application may soon become impossible to maintain. These applications typically require a better encapsulated data model that consists of a middle-tier data access layer or business component. So it's a wiser and more common practice to use the ObjectDataSource control.
2 ObjectDataSource Overview of
The ObjectDataSource Control object model is similar to the SqlDataSource control. ObjectDataSource exposes a TypeName property (rather than a ConnectionString property) that specifies the type of object (class name) to instantiate to perform the data operation. Similar to SqlDataSource command properties, ObjectDataSource controls support properties such as SelectMethod, UpdateMethod, InsertMethod, and DeleteMethod. A method that specifies the type of association to invoke to perform these data operations. This section describes methods for building data access and business logic layer components and exposing them through ObjectDataSource controls. The following is how the control is declared:
<asp:objectdatasource
Cacheduration= "String| Infinite "cacheexpirationpolicy=" absolute| Sliding "
Cachekeydependency= "string"
Conflictdetection= "overwritechanges| CompareAllValues "
Convertnulltodbnull= "true| False "Dataobjecttypename=" string
Deletemethod= "string" enablecaching= "true| False "
Enablepaging= "true| False "enabletheming=" true| False "
Enableviewstate= "true| False "filterexpression=" string
Id= "string" insertmethod= "string"
Maximumrowsparametername= "string"
Oldvaluesparameterformatstring= "string"
Ondatabinding= "DataBinding event handler"
Ondeleted= "Deleted event handler" ondeleting= "deleting event handler"
Ondisposed= "disposed event handler" onfiltering= "filtering event handler"
Oninit= "Init event handler" oninserted= "Inserted event handler"
oninserting= "inserting event handler" onload= "Load event handler"
Onobjectcreated= "objectcreated event handler"
Onobjectcreating= "ObjectCreating event handler"
Onobjectdisposing= "Objectdisposing event handler"
Onprerender= "PreRender event handler" onselected= "Selected event handler"
onselecting= "Selecting event handler" onunload= "Unload event handler"
Onupdated= "Updated event handler" onupdating= "Updating event handler"
runat= "Server" selectcountmethod= "string"
Selectmethod= "string" Sortparametername= "string"
Sqlcachedependency= "string" Startrowindexparametername= "string"
Typename= "string" updatemethod= "string"
>
<DeleteParameters>
<asp:controlparameter controlid= "string"
Convertemptystringtonull= "true| False "
Defaultvalue= "string"
Direction= "input| Output| Inputoutput| ReturnValue "
Name= "string"
Propertyname= "string"
Size= "Integer"
Type= "empty| Object| dbnull| boolean| char| sbyte|
byte| int16| uint16| int32| uint32| int64| uint64|
single| double| decimal| datetime| String "
/>
<asp:cookieparameter cookiename= "string"/>
<asp:formparameter formfield= "string"/>
<asp:parameter name= "string"/>
<asp:profileparameter propertyname= "string"/>
<asp:querystringparameter querystringfield= "string"/>
<asp:sessionparameter sessionfield= "string"/>
</DeleteParameters>
<filterparameters> ...</filterparameters>.
<insertparameters> ...</insertparameters>.
<selectparameters> ...</selectparameters>.
<updateparameters> ...</updateparameters>.
</asp:ObjectDataSource>
3 binding to the data access layer
The data access Layer component encapsulates Ado.net code to query and modify the database through SQL commands. It typically refines the details of creating ado.net connections and commands, and exposes these details by methods that can be invoked with the appropriate parameters. A typical data access layer component can be exposed as follows:
public class Mydatablllayer {
Public DataView getrecords ();
public int UpdateRecord (int recordid, String recorddata);
public int DeleteRecord (int recordid);
public int Insertrecord (int recordid, String recorddata);
}
That is, the operations that are typically defined on the database in the business logic access layer define the Getrecords, UpdateRecord, DeleteRecord, and Insertrecord four methods to read, update, delete, and insert data from the database. These methods are basically defined according to the Select, Update, delete, and INSERT statements in SQL.
Corresponding to the above method, ObjectDataSource provides four properties to set the data processing referenced by the control, which can be associated with the type as follows
<asp:objectdatasource typename= "Mydatalayer" runat= "Server"
Selectmethod= "Getrecords"
Updatemethod= "UpdateRecord"
Deletemethod= "DeleteRecord"
Insertmethod= "Insertrecord"
/>
The Selectmethon here is set to the Getrecords () method in Mydatablllayer, which needs to be noted when using ObjectDataSource to simplify the development of data in a declarative manner, So the value of setting SelectMethod here is getrecords instead of getrecords ().
Similarly, UpdateMethod, DeleteMethod, InsertMethod respectively correspond to the UpdateRecord
, DeleteRecord, Insertrecord method.
In the definition of getrecords () above, the reader can see that the type returned by the method is DataView, and since ObjectDataSource will need to be a data source for the bound control in the future, its return type must be one of the following return types:
Ienumerable, DataTable, DataView, DataSet, or object.
In addition, ObjectDataSource also has an important attribute Typename,objectdatasource