Asp. NET tip: The GridView control custom pagination Detailed

Source: Internet
Author: User
Tags implement prev tostring
Asp.net| Page | tips | control |

I wrote a few days ago about << implement pagination >> and <<gridview control events in stored procedures >>, then some people asked me how to apply this in the GridView! It's really simple, The main is how to save the page page pageindex problem, but to solve what is good to do. Because in the paging process: pagesize is certain, we can use a property to represent. Save PageIndex Many methods, and the data is not very large, Basically not good too much resources. Or an old saying, no more than an example of intuitive.

Here we'll use a hidden field to save this pageindex, the current page number. When you click on the previous page, you subtract its value by one, and you know it's 0, and note that the first page number is 0 instead of 1 here. Look at the code, and then we analyze the analysis!
1<asp:gridview id= "Newsgrid" runat= "Server" autogeneratecolumns= "false" allowpaging= "false" Width= "100%" >
2 <Columns>
3 <asp:boundfield datafield= "NewsId" headertext= "News id"/>
4 <asp:hyperlinkfield datanavigateurlfields= "NewsId" datanavigateurlformatstring= "~/details.aspx?id={0}"
5 datatextfield= "title" headertext= "News title" itemstyle-width= "70%"/>
6 <asp:boundfield datafield= "Posttime" headertext= "Release Time"/>
7 <asp:commandfield headertext= "News management" showcancelbutton= "False" showdeletebutton= "True"
8 showeditbutton= "True"/>
9 </Columns>
Ten </asp:GridView>
One <div style= "HEIGHT:16PX; padding-top:5px; margin-right:30px; Float:right ">
<asp:hiddenfield id= "currentpage" runat= "server" value= "0"/>
<asp:linkbutton id= "First" runat= "Server" commandargument= "frist" > Home </asp:LinkButton>
<asp:linkbutton id= "Prev" runat= "Server" commandargument= "Prev" > Prev </asp:LinkButton>
<asp:linkbutton id= "Next" runat= "Server" commandargument= "Next" > next page </asp:LinkButton>
<asp:linkbutton id= "Last" runat= "Server" commandargument= "final" > End </asp:LinkButton>
</div> CS File Code:
1 protected void Pagerbutton_click (object sender, EventArgs e)
2 {
3 int pageindx = Convert.ToInt32 (Currentpage.value);
4 int totals = newsmanager.getnews (0, PageSize). totalrecords;
5 int pages = (totals% pageSize) = = 0? (totals/pagesize): (totals/pagesize + 1);
6 String arg = ((LinkButton) sender). Commandargument.tostring (). ToLower ();
7 Switch (ARG)
8 {
9 Case "Prev":
Ten if (Pageindx > 0)
11 {
Pageindx-= 1;
13}
break;
Case "Next":
if (Pageindx < pages-1)
17 {
Pageindx + 1;
19}
break;
Case "Last":
Pageindx = pages-1;
break;
Default:
pageindx = 0;
-Break;
27}
Currentpage.value = Pageindx.tostring ();
Newsgrid.datasource = Newsmanager.getnews (Pageindx, pageSize). entities;
Newsgrid.databind ();
31}

is not very simple ah, read the code are understood, about using the button to pass the parameters here, there is not much to say, there is a entities attribute, in the << application entity class EntitySet implement similar paradigm function >> this article can find



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.