Preface:
When learning ASP. net2.0, we found that Microsoft provided several data source controls when operating data.
However, each control has its own advantages and disadvantages. For example, it is convenient to use sqldatasource.
However, in our projects, we often need to develop projects in different layers. So I seldom use sqldatasource, so I began to pay attention to objectdatasource.
However, I found many problems when using it. I did not find any good answers on the Internet. Most of my predecessors asked me that I never used this item. ^_^
In fact, I do not need to use the previous method. however, for the purpose of the study, I tried it myself. the following content describes the problems encountered during learning and finds the solutions. If you are learning this objectdatasource, you can see what is the difference between my solution and your solution. if you have a good idea, you must sue me.
Part 1 display and sorting using objectdatasource
Here I am using a generic set, so when I make a query, the returned data is not dataset, so I will first compile the method of combining the returned generic data in the Dal.
# Region: public static sortablelist <userinfo> getuserinfolist () {sortablelist <userinfo> List = new sortablelist <userinfo> (); Using (sqldatareader DR = database. executereader (database. connectionstring, commandtype. text, "select * From userinfo") {While (dr. read () {userinfo info = new userinfo (); info. id = convert. toint16 (Dr ["ID"]); info. uname = Dr ["uname"]. tostring (); info. age = convert. toint16 (Dr ["Age"]); info. sex = (INT) Dr ["sex"]; List. add (Info) ;}} return list ;}# endregion
Through the above Code, a collection of userinfo is returned.
To enable objectdatasource to directly find the classes and methods used for processing, you need to add relevant attribute descriptions to the classes and methods.
Using system; using system. collections. generic; using system. text; using system. componentmodel; // first reference the relevant namespace using models; namespace BLL {[dataobject ()] // Add the description public class userinfobll {[dataobjectmethod (dataobjectmethodtype) to the class. select)] // The objectdatasource is used to query the public static list <userinfo> getuserinfolist (string sort) {sortablelist <userinfo> List = Dal. userinfodal. getuserinfolist (); If (! Sort. Equals ("") {bool flag = true; If (sort. indexof ("DESC ")! =-1) {flag = false;} List. sort (sort. replace ("DESC", ""), flag);} return list;} [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);} public static void deletemulutiuserinfo (string userlist) {Dal. userinfodal. deletemulutiuserinfo (userlist );}}}
Then you can add the gridview and objectdatasource controls on your ASP. NET page. The rest is to configure your data source.
Select your select method.
If you do not set the input sort parameter required for the next step, follow the default method.
Then you can run it happily and check out. showshow my results.
If there is no problem, let's sort it next, because we have added a sort parameter when writing the background code, so this is simple.
First, set a property sortparamtername of objectdatasource. Here we set it to the passed-in parameter sort in BLL.
Then you can enable the sorting content.
Now we can implement sorting. of course, I re-wrote a sortlist generic set here. for common applications, you can directly change your SQL statements to implement this sorting function. it should be the column name that will be passed in when you click a column for the first time, such as ID, and Id DESC will be passed in when you click a column for the second time.