One-step learning of the objectdatasource control-custom paging sorting

Source: Internet
Author: User
One-step learning of the objectdatasource control-custom paging sorting

In previous http://mqingqing123.cnblogs.com/archive/2006/04/07/369020.html
Describes the general usage of objectdatasource.

Last time, a user wanted to introduce the problem of custom paging. This article describes how to use objectdatasource to customize paging and sorting. You will find that objectdatasource is highly scalable ,. Whether it's a beginner or experienced user, objectdatasource can always provide you with functions that meet your requirements.

In data paging, the simplest is to use the pagination and sorting functions of the gridview. This function should not be writtenCode, Which can be paged and sorted by a slight check. However, this method can be mitigated when the data volume is small.ProgramWhen there is a large amount of data, such as hundreds of thousands or even hundreds of thousands of records, using the system's built-in paging will lead to a large amount of data recovery, so using custom paging is more effective.

In summary, the daily summary of Custom Data paging mainly includes four methods:
1) use temporary tables-this method is widely used in forums such as communityserver and blogSource code
2) use stored procedures-This method seems to have come from an article on csdn. You can view an article in the blog circle at the following URL.Article
Http://genson.cnblogs.com/archive/2006/01/17/318882.html
3) use SQL statements to select the limited data paging. I have used this method and I feel that there are some problems that need further confirmation.
4) You can use the client-to-server callback of girdview to obtain data on the new page. This is a new feature of ASP. net2.0.

This article mainly introduces the first method of using temporary tables for paging. Other methods will be introduced later

The following is a change to the above article. The Code is as follows:

Public List<Product> Loadallproduct (IntStartindex,IntMaxrows,StringSortedby)

{

List<Product> Products =New List<Product> ();

SqlconnectionConn =New Sqlconnection(Configurationmanager. Connectionstrings ["Connectionstring"]. Connectionstring );

StringCommandtext =@"

-- Create a temporary table for the page

Create Table # temppagetable

(

Indexid int identity (0, 1) not null,

Id int

)

 

-- Read data and insert a temporary table

Insert into # temppagetable

(

[ID]

)

 Select

[Productid]

From products";

 

 

 

If(Sortedby! ="")

{

Commandtext + ="Order"+ Sortedby;

}

 

Commandtext + =@"

Set @ totalrecords = @ rowcount

 

 

Select

SRC. [productid],

SRC. [productname],

SRC. [categoryid],

SRC. [price],

SRC. [instore],

SRC. [description]

From products SRC, # temppagetable P

Where

SRC. [productid] = P. [ID] and

P. indexid >=@ startindex and P. indexid <(@ startindex + @ maxrows )";

If(Sortedby! =""){

Commandtext + ="Order"+ Sortedby;

}

 

SqlcommandCommand =New Sqlcommand(Commandtext, Conn );

Command. Parameters. Add (New Sqlparameter("@ Startindex", Startindex ));

Command. Parameters. Add (New Sqlparameter("@ Maxrows", Maxrows ));

Command. Parameters. Add (New Sqlparameter("@ Totalrecords",Sqldbtype. INT ));

Command. Parameters ["@ Totalrecords"]. Direction =Parameterdirection. Output;

 

Conn. open ();

SqldatareaderDr = command. executereader ();

While(Dr. Read ()){

ProductProd =New Product();

 

Prod. productid = (Int) Dr ["Productid"];

Prod. productname = (String) Dr ["Productname"];

Prod. categoryid = (Int) Dr ["Categoryid"];

Prod. Price = (Decimal) Dr ["Price"];

Prod. instore = (Int16) Dr ["Instore"];

Prod. Description = (String) Dr ["Description"];

Products. Add (prod );

}

 

Dr. Close ();

Conn. Close ();

 

_ COUNT = (Int) Command. Parameters ["@ Totalrecords"]. Value;

 

ReturnProducts;

}

 

 

Public IntCountall ()

{

Return_ Count;

}

A simple explanation is as follows:
1) A temporary table # temppagetable is defined here. "#" is required for the definition of a temporary table. The advantage of a temporary table is that it is automatically created and destroyed when it is not needed. For more information, see the Help System of SQL.
2) In the temporary table, I created an index print column indexid and ID column. If you look at the design of the producst table in my database, you can see that the table contains a productid column, which is an auto-incrementing ID seed, why do you need to create an indexid column?
This is because the productid column of the product table is an auto-incrementing form, so the sequence number may be disrupted during editing. For example, the original product record is, 5. After you delete a record, for example, 5, when you add another record, the new serial number starts from 6 instead of 5.
To enable auto-increment of index numbers, a custom temporary indexid table is used.
3) The ID column in the temporary table corresponds to productid. As you can see, the data inserted in this ID column actually comes from the productid column in the products table.


The source code used on the page is as follows:

<ASP:Objectdatasource ID= "Objectperformance1" Runat= "Server" Selectmethod= "Loadallproduct" Typename= "Productbll" Dataobjecttypename= "Product"

Enablepaging= "True" Maximumrowsparametername= "Maxrows" Startrowindexparametername= "Startindex" Selectcountmethod= "Countall" Sortparametername= "Sortedby"

> </ASP:Objectdatasource>

& Nbsp;<ASP:Gridview ID= "Gridview1" Runat= "Server" Cellpadding= "4" Font-names= "Verdana"

Font-size= "X-small" Forecolor= "#333333" Gridlines= "NONE" Performanceid= "Objectperformance1" Allowpaging= "True" Allowsorting= "True">

<Footerstyle Backcolor= "#1c5e55" Font-bold= "True" Forecolor= "White" />

Please study the specific explanations.

Click here to download the source code:/Files/mqingqing123/objectdatasourceexample.rar

Use vs. net2005 or vwd2005 to open the file system. You can directly run the source code of this article.
 

Posted on every day read (1668) Comments (7) EDIT favorite reference network Abstract category: ASP. NET V2.0


Feedback:

# Re: one-step learning of the objectdatasource control-custom paging sorting | wow players <asp: objectdatasource id = "objectdatasource1" runat = "server" selectmethod = "loadallproduct" typename = "productbll" dataobjecttypename = "product"

Enablepaging = "true" maximumrowsparametername = "maxrows" startrowindexparametername = "startindex" selectcountmethod = "countall" sortparametername = "sortedby"

> </ASP: objectdatasource>

Can you explain the general meaning of the attribute listed above objectdatasource ?? Reply to more comments
# Re: Learn the objectdatasource control step by step -- customize page sorting | life! Refer to this article
Http://rhs.cnblogs.com/articles/329648.html replies to more comments
# Re: Learn the objectdatasource control step by step -- customize paging sorting | does YSP explain how these two parameters are passed? Maximumrowsparametername = "maxrows" startrowindexparametername = "startindex" Reply to more comments
# Re: one-step learning of the objectdatasource control-custom paging sorting | when using objectdatasource to customize pagination every day, in addition to the allowpaging = "true" of the gridview, you also need to set objectdatasource to "enablepaging =" true ". In this case, objectdatasource does not automatically maintain paging information, so we need to set paging processing by ourselves.
Custom paging has three main considerations:
1) number of records on each page
2) Total number of records
3) What is the current page?
In this way, three parameters are required, but the first one has been completed by the gridview. The default pagesite of the gridview is 10, that is, each page has 10 records by default.
The objectdatasource is required when you buy two functions.
1) selectcountmethod, just like its name, gets the number of records in the database. Of course, you can simply think of this number as select count (*) from database
2) the current page of startrowindexparametername setting
The above two functions are actually the same as the custom pages of the DataGrid. I have mentioned in detail in ASP. NET technical details and application instances.
The next step is the new parameter processing.
Now that you know the function of the parameter, how to pass it to the loadallproduct function is as follows:
Public list <product> loadallproduct (INT startindex, int maxrows, string sortedby)
There are two parameters: startindex and maxrows, which respectively set the current page and total number of records.
(The other is sorting)
The names of these parameters are important. You may want to set loadallproduct
Public list <product> loadallproduct (INT start, int Max, string sortedby)
This will cause an error because the objectdatasource uses the parameter defined in the Declaration method, so the parameter here must be consistent with the value specified by startrowindexparametername and maximumrowsparametername.
(The order of parameters does not matter.) reply to more comments.
# Re: Learn the objectdatasource control step by step -- customize paging sorting | does the Code magicbeans use pagination of the type you described?

Public static datatable query (string sqlstring, int startrecord, int maxrecords)
{
Using (sqlconnection connection = new sqlconnection (connectionstring ))
{
Datatable dt = new datatable ();
Try
{
Connection. open ();
Sqldataadapter command = new sqldataadapter (sqlstring, connection );
Command. Fill (startrecord, maxrecords, DT );
}
Catch (system. Data. sqlclient. sqlexception ex)
{
Throw new exception (ex. Message );
}
Return DT;
}
} Reply to more comments
# Re: Learn the objectdatasource control step by step -- customize paging sorting | a problem occurs when a mortal uses the objectdatasource control: the returned value cannot be obtained.
For example, if you select data to return the total number of pages, set direction to "output". But how can you obtain the value of totalpages? I found a lot of information and didn't introduce it.
<Selectparameters>
<Asp: parameter name = "totalpages" type = "int32" direction = "output"/>
</Selectparameters>
The Code is as follows:
<Selectparameters>
<Asp: controlparameter name = "totalpages" type = "int32" controlid = "totalpages" direction = "output" propertyname = "text"> </ASP: controlparameter>
</Selectparameters>
Please advise. Thank you! Reply to more comments

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.