Some new data controls in ASP. NET 2.0, such as gridview, detailsview, and formview, can be added, modified, or deleted directly after they are bound to sqldatasource without coding. That is to say, these controls call the method corresponding to sqldatasource internally to implement corresponding operations. However, some conditions are not suitable for using these controls, but common controls are required. For example, sometimes datalist or reapter can only be used for data input maintenance using a few textbox. For example, to insert a record to a database table, however, all the fields of this data are not aggregated on the Interface display. For example, the header of a page contains several fields, the backend contains several fields, and some other controls exist in the middle, it corresponds to other data. In this case, it is difficult to use the detailsview control or formview control, but we can only place the control on our own. In this case, we can certainly not use the sqldatasource control, but follow the conventional method, for example, a method returns an object and binds each textbox to this object. The advantage of using sqldatasource is that it can reduce the amount of code, and ASP. NET 2.0 uses the datasouce control to perform data operations, so it is best to use it. Although it still requires a certain amount of code to bind a common control with sqldatasource, it is still much less.
The following code snippets show how to use sqldatasource to bind a common data control:
1. Bind textbox to display
Datasourceselectarguments AR = new datasourceselectarguments ();
Dataview DV = (dataview) sqldatasource1.select (AR );
This.txt closingparagraph. Text = DV. Table. Rows [0] ["closingparagraph"]. tostring ();
This.txt openingparagraph. Text = DV. Table. Rows [0] ["openingparagraph"]. tostring ();
This.txt createddate. Text = DV. Table. Rows [0] ["createddate"] = dbnull. value? String. Empty: (datetime) dv. Table. Rows [0] ["createddate"]). to1_datestring ();
This.txt createdby. Text = DV. Table. Rows [0] ["createdby"]. tostring ();
This.txt disclaimer. Text = DV. Table. Rows [0] ["disclaimer"]. tostring ();
This.txt openingparagraph. Text = DV. Table. Rows [0] ["openingparagraph"]. tostring ();
This.txt quoteattn. Text = DV. Table. Rows [0] ["quoteattn"]. tostring ();
This.txt quoteno. Text = isnew? Datetime. Now. ticks. tostring (): DV. Table. Rows [0] ["quoteno"]. tostring ();
This.txt quoteowner. Text = DV. Table. Rows [0] ["quoteowner"]. tostring ();
This.txt quoteto. Text = DV. Table. Rows [0] ["quoteto"]. tostring ();
This.txt subject. Text = DV. Table. Rows [0] ["subject"]. tostring ();
This.txt templateid. Text = DV. Table. Rows [0] ["templateid"]. tostring ();
2. bind to datalist and insert
Sqldatasource DATA = (sqldatasource) grid. Parent. findcontrol ("sql1_c4 ");
Data. insertparameters ["quoteitemsurchargeid"]. defaultvalue = guid. newguid (). tostring ();
Data. insertparameters ["quoteitemid"]. defaultvalue = data. selectparameters [0]. defaultvalue;
Data. insertparameters ["tariffname"]. defaultvalue = (textbox) grid. footerrow. findcontrol ("txttariffname"). Text. Trim ();
Data. insertparameters ["rate20"]. defaultvalue = (textbox) grid. footerrow. findcontrol ("txtrate20"). Text. Trim ();
Data. insertparameters ["rate40"]. defaultvalue = (textbox) grid. footerrow. findcontrol ("txtrate40"). Text. Trim ();
Data. insertparameters ["rate40hc"]. defaultvalue = (textbox) grid. footerrow. findcontrol ("txtrate40hc"). Text. Trim ();
Data. insertparameters ["rate45"]. defaultvalue = (textbox) grid. footerrow. findcontrol ("txtrate45"). Text. Trim ();
Data. insertparameters ["createddate"]. defaultvalue = datetime. Now. tostring ();
Data. insert ();
This. datalist1.databind ();
3. bind to datalist and update
Sqldatasource2.updateparameters ["Origin"]. defaultvalue = (textbox) E. Item. findcontrol ("txtorigin"). Text. Trim ();
Sqldatasource2.updateparameters ["destination"]. defaultvalue = (textbox) E. Item. findcontrol ("txtdestination"). Text. Trim ();
Sqldatasource2.updateparameters ["rate20"]. defaultvalue = (textbox) E. Item. findcontrol ("txtrate20"). Text. Trim ();
Sqldatasource2.updateparameters ["rate40"]. defaultvalue = (textbox) E. Item. findcontrol ("txtrate40"). Text. Trim ();
Sqldatasource2.updateparameters ["rate40hc"]. defaultvalue = (textbox) E. Item. findcontrol ("txtrate40hc"). Text. Trim ();
Sqldatasource2.updateparameters ["rate45"]. defaultvalue = (textbox) E. Item. findcontrol ("txtrate45"). Text. Trim ();
Sqldatasource2.updateparameters ["etddate"]. defaultvalue = (textbox) E. Item. findcontrol ("txtetddate"). Text. Trim ();
Sqldatasource2.updateparameters ["carrier"]. defaultvalue = (textbox) E. Item. findcontrol ("txtcarrier"). Text. Trim ();
Sqldatasource2.updateparameters ["TT"]. defaultvalue = (textbox) E. Item. findcontrol ("txttt"). Text. Trim ();
Sqldatasource2.updateparameters ["quoteitemid"]. defaultvalue = (datalist1.datakeys [E. Item. itemindex]). tostring ();
Sqldatasource2.update ();
Datalist1.edititemindex =-1;
Datalist1.databind ();
In fact, it is mainly to manually call the select () Inert () Update () method of sqldatasource. For other details, refer to the methods corresponding to msdn sqldatasource. msdn has specific implementation instances.