Tip/TRICK: implementcopyfunctionwithformviewandsql1_c

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.