When processing user input, there are often many similar input entries. You want to complete the new input by copying the original record and slightly modifying it. Of course, this is a simple function and is not difficult to implement. However, if you are using ASP. NET 2.0 Data Control and datasource binding method, if you can try to achieve this function in this mode, try to enjoy ASP. NET 2.0 brings us convenience and reduces the amount of encoding, that's great.
Previously, this function was used to copy records in SQL statements and modify the records on the interface. That is to say, after you click a copy button, the new record has been actually created. When you click Save, you only modify it, even if you do not save the record. Obviously, this does not implement the real intention of the user.
Another method is to use formview as an example. Formview has three view modes: View, edit, and insert. To implement the copy function, you can actually use its editing interface, but the insert function can be executed in the background. In this case, the problem is simple. In the updating event of formview or the updating event of its corresponding datasource control, we only need to judge that if the copy operation is executed, the insert operation will be executed, the following are key code snippets:
Protected void sqldatasourcefreetariffdetail_updating (Object sender, sqldatasourcecommandeventargs E)
{
If (iscopying)
{
Guid key = guid. newguid ();
E. Command. commandtext = sqldatasourcefreetariffdetail. insertcommand;
E. Command. Parameters ["@ ID"]. value = key;
E. Command. Parameters ["@ createdby"]. value = user. Identity. Name;
E. Command. Parameters ["@ createddate"]. value = datetime. now;
Iscopying = false;
}
}
Note that the updateparameters and insertparameters must be consistent in the page code, and some parameter values are different from the update operation, so there must be several lines of red code.
In this way, the Data Binding and data operation modes of ASP. NET 2.0 are basically used to the maximum extent, and this function is implemented with the minimum amount of code.