Previously, in this article, I have used a rough and simple summary of a paging helper class in Asp.net based on my previous experience, more than one people leave a message and complain about how to use it.ArticleAll of them are summary of what they think, and they do not take into account the reading experience ). The purpose of this article is to re-introduce the use of this auxiliary method. At the same time, we will explain that we often use a paging auxiliary control different from the one in the previous article at the front end. If you still don't understand how to use them, download the demo in this article and check it out.
1. engineering and project structure
In the Dal layer, we mainly control and implement data access through ibatisnet. If you are not clear about ibatisnet, you can imagine that ibatisnet is used to return a generic data list set based on the query conditions.
2. Page-based parameter passing for paging Query
Example in this articleCodeIn, enter firstname and (or) lastname to fuzzy match and query some people, and display them through a gridview page. The process of passing parameters is very important. After inputting parameters in two TextboxProgramTo analyze whether to click a button or directly click a number for paging query, the following page load data initialization process is very important:
Protected int currentpg = 1; protected int totalcount = 0; protected int recordsperpg = 10; private int leftsize = 3; private ilist <person> listperson = NULL; protected void page_load (Object sender, eventargs e) {If (ispostback = false & string. compare (request. requesttype. tolower (), "get") = 0) {If (string. isnullorempty (request ["pageindex"]) = false & regutil. ispositivenumber (request ["pageindex"]) {currentpg = int. parse (request ["pageindex"]);} If (string. isnullorempty (request ["firstname"]) = false) {this.txt firstname. TEXT = request ["firstname"];} If (string. isnullorempty (request ["lastname"]) = false) {this.txt lastname. TEXT = request ["lastname"];} bindpager (); // bind the result and display the page }} 3. display effects of two paging auxiliary classes
I inserted some data in the person table. Then, we use the aspnetpager class and aspnetpagercontrol class under DOTNET. Common. webform to display the page:
(1 ),Call of the aspnetpager helper class
On the pagertest. ASPX page, first reference a namespace <% @ import namespace = "DOTNET. Common. webform" %> and write the following code:
<% Aspnetpager. rendertablepager (pagermodoule. Statistics, tbalignenum. Left, response, totalcount, currentpg, recordsperpg, String. Format ("pagertest. aspx? Firstname = {0} & lastname = {1} ", server. urlencode (txtfirstname. text. trim (), server. urlencode (txtlastname. text. trim (); %>
Is this hard to understand? It does not directly generate paging strings, but directly writes data to the current context through the respose object. How do I pass two names as parameters to the page? This line:
String. Format ("pagertest. aspx? Firstname = {0} & lastname = {1} ", server. urlencode (txtfirstname. Text. Trim (), server. urlencode (txtlastname. Text. Trim ()))
You can call the data query at the Dal layer by passing in the pagertest. ASPX page and its two input text boxes.
In fact, this method is not the most intuitive and context-independent Writing Method (many parameters are passed), so we will introduce the following aspnetpagercontrol auxiliary method.
(2 ),Call of the aspnetpagercontrol auxiliary class
This helper class is similar to the paging EFFECT oF comments on Netease news channels. The blog Park is also similar to the paging display method. Many people have discussed it before and it is easy to call.
A. initialize a control first.(This article writes the control pagination binding in the bindpager method ):
String basestring = string. Format ("pagertest. aspx? Firstname = {0} & lastname = {1} ", server. urlencode (txtfirstname. text. trim (), server. urlencode (txtlastname. text. trim (); aspnetpagercontrol control = new aspnetpagercontrol (currentpg, recordsperpg, totalcount, leftsize, basestring );
Style is very important during initialization. We usually configure a node in Web. config:
<Deleetask> <! -- Pagination style --> <add key = "pagerstyle" value = "pagercss"/> </appsettings>
When a new object is created, use the following section:
Try {// style cssstyle = configurationmanager. appsettings ["pagerstyle"]; If (string. isnullorempty (cssstyle) {Throw new argumentnullexception ("there is no pagerstyle configuration info in config file... ") ;}} catch {cssstyle =" default ";}
Assign values to the cssstyle attribute of the current object, and use this style each time to unify the paging style. Pagercss in the demo in this article is relatively simple, and I am not very sensitive to front-end styles. You can write CSS by yourself or with front-end engineers to redefine their display effects.
B. Passing parameters and rendering
The process of passing parameters is very important. We need to transmit the parameters to the constructed basestring:
String basestring = string. Format ("pagertest. aspx? Firstname = {0} & lastname = {1} ", server. urlencode (txtfirstname. Text. Trim (), server. urlencode (txtlastname. Text. Trim ()));
Isn't this the last parameter of the rendertablepager method in the aspnetpager helper class? Its paging page effect is similar to the aspnetpager auxiliary class. It can also be achieved through the renderstatisticpager and renderpager methods with or without statistical information.
Let's take a look at the effect (the first line is the aspnetpager, and the second line is the aspnetpagercontrol ):
I spent a little time reading technical books every day in the last phase. I found that reading books directly is more comfortable than having to drive a computer around the West or just knock on the code, because reading a book can help you get rid of the implications and cloud inertia, so that you can learn to think and summarize yourself, and then contact your development experience to improve your work, at the same time, it can guide you to accomplish something better and more deeply. I would like to say a little more. Actually, there is no great use for anyone who only needs to use the paging function. You should download the demo, take a closer look at the source code, and think more about it, comprehend the Implementation ideas and try it out.
Download the sample: ibatiswebapp