Reprinted please indicate the source: http://surfsky.cnblogs.com/
---------------------------------------------------------
-- Data sources supported by datasource
-- Datatable
-- Ilist
-- Bindinglist
-- Xxxdatasource
---------------------------------------------------------
Datatable
Grid. datasource = DT;
Grid. databind ();
Ilist
Int ArticleID = convert. toint32 (request. querystring ["ArticleID"]);
Ilist <blogarticleimage> images = blogarticleimage. listarticleimages (ArticleID );
This. gvimages. keyfieldname = "imageid ";
This. gvimages. datasource = images;
This. gvimages. databind ();
Bindinglist
Private void createquotes ()
{
Bindinglist <quote> res = new bindinglist <quote> ();
Foreach (string name in names)
{
Quote q = new quote (name );
Q. value = (decimal) getrandom (). Next (800,200 0)/(decimal) 10;
Res. Add (Q );
}
Session ["quotes"] = res;
}
Accessdatasource
<Asp: accessdatasource id = "accessdatasource1" runat = "server" datafile = "~ /App_data/nwind. mdb"
Selectcommand = "select * from [MERs]"
Deletecommand = "delete from [MERs] Where [customerid] =? "
Insertcommand = "insert into [MERs] ([customerid], [companyName], [contactname], [contacttitle], [address], [City], [region], [postalcode], [country], [PHONE], [Fax]) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "
Updatecommand = "Update [MERs] Set [companyName] = ?, [Contactname] = ?, [Contacttitle] = ?, [Address] = ?, [City] = ?, [Region] = ?, [Postalcode] = ?, [Country] = ?, [Phone] = ?, [Fax] =? Where [customerid] =? "
/>
<Asp: accessdatasource id = "accessdatasource1" runat = "server" datafile = "~ /App_data/nwind. mdb"
Ondeleting = "accessdatasource1_modifying" oninserting = "accessdatasource1_modifying" onupdating = "accessdatasource1_modifying"
Selectcommand = "select * from [MERs]" deletecommand = "delete from [Customers] Where [customerid] =? "
Insertcommand = "insert into [MERs] ([customerid], [companyName], [contactname], [contacttitle], [address], [City], [region], [postalcode], [country], [PHONE], [Fax]) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "
Updatecommand = "Update [MERs] Set [companyName] = ?, [Contactname] = ?, [City] = ?, [Region] = ?, [Country] =? Where [customerid] =? ">
<Deleteparameters>
<Asp: parameter name = "customerid" type = "string"/>
</Deleteparameters>
<Updateparameters>
<Asp: parameter name = "companyName" type = "string"/>
<Asp: parameter name = "contactname" type = "string"/>
<Asp: parameter name = "city" type = "string"/>
<Asp: parameter name = "region" type = "string"/>
<Asp: parameter name = "country" type = "string"/>
</Updateparameters>
</ASP: accessdatasource>
Protected void accessdatasource1_modifying (Object sender, sqldatasourcecommandeventargs e ){
Demosettings. assertnotreadonly ();
}
Implement with code
Accessdatasource DS = new accessdatasource ();
DS. datafile = accessdatasource1.datafile;
DS. selectcommand = "select photo from [employees] Where employeeid =" + ID;
Dataview view = (dataview) ds. Select (datasourceselectarguments. Empty );
If (view. Count> 0) return view [0] [0] As byte [];
Return NULL;
Objectdatasource
<Asp: objectdatasource id = "objectperformance1" runat = "server"
Typename = "quotes"
Selectmethod = "loadquotes"
/>
<Asp: objectdatasource id = "objectperformance1" runat = "server"
Dataobjecttypename = "personregistration"
Typename = "mypersonprovider"
Selectmethod = "getlist" updatemethod = "Update" insertmethod = "insert"
/>
<Asp: objectdatasource id = "objectperformance1" runat = "server"
Typename = "personmanager"
Selectmethod = "selectpersons"
Deletemethod = "deleteperson"
Updatemethod = "updateperson"
Insertmethod = "insertperson">
<Insertparameters>
<Asp: parameter name = "ID" type = "int32"/>
</Insertparameters>
</ASP: objectdatasource>
Objectdatasource. Parameters
<Selectparameters>
<Asp: sessionparameter name = "igysid" sessionfield = "ID" type = "int32"/>
<Asp: sessionparameter defaultvalue = "0" name = "icgfs" sessionfield = "icgfs" type = "int32"/>
</Selectparameters>
<Updateparameters>
<Asp: parameter name = "iwzid"/>
<Asp: parameter name = "igysid"/>
<Asp: parameter name = "icgmxid"/>
<Asp: parameter name = "igysbjid"/>
<Asp: parameter name = "nbj"/>
<Asp: parameter name = "CBZ"/>
</Updateparameters>
<Updateparameters>
<Asp: formparameter formfield = "makeid" name = "makeid" type = "string"/>
<Asp: formparameter formfield = "name" name = "name" type = "string"/>
<Asp: formparameter formfield = "ID" name = "ID" type = "string" convertemptystringtonull = "false"/>
</Updateparameters>
Classes used by objectdatasource
(The following code is collated and modified to the ASP. NET 2.0 revealed P71-P78)
(For an example of another shopping basket, refer to P138)
Public class person
{
Private int ID;
Private string firstname;
Private string lastname;
Public int ID {...}
Public String firstname {...}
Public String lastname {...}
Public Person (int id, string firstname, string lastname)
{
This. ID = ID;
This. firstname = firstname;
This. lastname = lastname;
}
}
Public class personcollection: List <person> {}
Public class personmanager
{
Private const string personskey = "persons ";
Public personcollection selectpersons ()
{
Httpcontext context = httpcontext. Current;
If (context. application [personkey] = NULL)
{
Personcollection persons = new personcollection ();
Persons. Add (new person (0, "Patrick", "Lorenz "));
Persons. Add (new person (0, "Patrick", "Lorenz "));
Persons. Add (new person (0, "Patrick", "Lorenz "));
Persons. Add (new person (0, "Patrick", "Lorenz "));
Context. application [personkey] = Persons;
}
Return context. application [personkey] As personcollection;
}
Public Person selectperson (int id)
{
Foreach (person P in selectpersons ())
If (P. ID = ID)
Return P;
Return NULL;
}
Public void deleteperson (int id)
{
Personcollection persons = (application [personkey] As personcollections );
Person = selectperson (ID );
If (person! = NULL)
Persons. Remove (person );
}
Public void insertperson (int id, string firstname, string lastname)
{
Personcollection persons = (application [personkey] As personcollections );
Persons. Add (new person (ID, firstname, lastname ));
}
Public void updateperson (int id, string firstname, string lastname)
{
Person = selectperson (ID );
If (person! = NULL)
{
Person. firstname = firstname;
Person. lastname = lastname;
}
}
}