Use objectdatasource Data Control and gridview to operate data in ASP. NET

Source: Internet
Author: User

We have implemented display data with pagination and sorting through the previous two contents, so how can we use objectdatasource to help us achieve deletion and modification?

The old method is to implement the method of operating data in the Dal layer and BLL layer first.

        [DataObjectMethod(DataObjectMethodType.Insert)]        public static void InserUserInfo(UserInfo info)        {            DAL.UserInfoDAL.InsertUserInfo(info);        }        [DataObjectMethod(DataObjectMethodType.Update)]        public static void UpdateUserInfo(UserInfo info)        {            DAL.UserInfoDAL.UpdateUserInfo(info);        }        [DataObjectMethod(DataObjectMethodType.Delete)]        public static void DeleteUserInfo(UserInfo info)        {            DAL.UserInfoDAL.DeleteUserInfo(info);        }

 

Here we can find that all the objects passed here are userinfo objects. as for the benefits, I think it is more secure for you to process attributes when assigning values. anyway, I always think it is better to pass a lot of variables. this method is used here.

If you want to use this method, pay attention to whether an attribute in your objectdatasource is correctly set.

Here I set it to the userinfo object I found.

In this way, you can delete and modify it. Some may ask! I didn't set the passed parameters as before. ^_^

Here, when you set the dataobjecttypename of objectdatasource, when you perform related operations, objectdatasource will automatically generate an object you set and pass the values in the row to your object one by one. then directly pass this object into your BLL method. however, there are still many traps.

The first trap is caused by data conflicts.

There is an attribute named struct in objectdatasource.

Here, the attribute is used to set whether to process the new and old values together.

The description on msdn is: Get or set a value, which determines whether to pass the new value only to the update method, or pass the old value and new value to the update method.

When the default overwritechanges is used here, the value is changed for you. Here the modified object value is passed. Therefore, your update is normal by default.

However, if you want to process the new and old values, you need to set compareallvalues. However, when you update the data, an error will be reported.

Why? When you set it to compareallvalues, you need to pass two objects to the BLL layer. One is the original userinfo and the other is the modified userinfo.

So you need to change this method.

        [DataObjectMethod(DataObjectMethodType.Update)]        public static void UpdateUserInfo(UserInfo info,UserInfo original_info)        {            ......        }

Here we find that our update method has an additional parameter named original_info, so that we can correctly update the modified content back to the database, and the original _ prefix here can be modified

.

Trap 2: Key Field Problems.

When conflicdetection uses the default overwritechanges value, we will delete it. we found that the record we want to operate was not deleted. then, through monitoring, we can find that the userinfo object passed to BLL is not empty. however, the attribute values are all default values.

Why is this problem? In my opinion, the delete operation does not modify the record in my gridview. Therefore, when conflicdetection uses the default overwritechanges value, if there is a value in the object passed, it can only be the modified value. therefore, if you want to delete records by ID, you can only delete records with a value of 0. pai_^

Therefore, the content you want cannot be deleted, but there is no error.

What should we do? Here, because we use a set, we do not set an attribute when writing the gridview, that is

Here we add the ID of the upper key field. Let's see the effect.

Although the userinfo object is not completely passed here, the ID value we want is. ^_^.

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.