A few days ago, I wrote about <<Implementing paging in stored procedures>> and <<Detailed explanation of GridView control events>>. Later, some people asked me how to apply this stuff in GridView! In fact, it is very simple, mainly how to There is a problem with saving the page number PageIndex of the current page, but everything is easier to solve. Because in the paging process: PageSize is certain, we can use an attribute to represent it. There are many ways to save PageIndex, and the data is not very large. Basically, there won’t be many resources. As an old saying goes, no matter how much words are said, there are no examples that are intuitive.
Here we will use a hidden field to save the PageIndex, which is the current page number. When the previous page is clicked, its value will be decremented. First, it is known to be 0. One thing to note is that the page number of the first page here is 0 instead of 1. Let’s take a look at the code, and then we will analyze it!
1<asp:GridView ID="NewsGrid" runat="server" AutoGenerateColumns="False" AllowPaging="false" Width="100%">
2 <Columns>
3 <asp:BoundField DataField="NewsId" HeaderText="NewsID"/>
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="Post Time"/>
7 <asp:CommandField HeaderText="News Management" ShowCancelButton="False" ShowDeleteButton="True"
8 ShowEditButton="True"/>
9 </Columns>
10 </asp:GridView>
11 <div style=" height:16px; padding-top:5px; margin-right:30px; float:right">
12 <asp:HiddenField ID="CurrentPage" runat="server" Value="0"/>
13 <asp:LinkButton ID="First" runat="server" CommandArgument="first" OnClick="PagerButton_Click">Home</asp:LinkButton>
14 <asp:LinkButton ID="Prev" runat="server" CommandArgument="prev" OnClick="PagerButton_Click">Previous page</asp:LinkButton>
15 <asp:LinkButton ID="Next" runat="server" CommandArgument="next" OnClick="PagerButton_Click">Next Page</asp:LinkButton>
16 <asp:LinkButton ID="Last" runat="server" CommandArgument="last" OnClick="PagerButton_Click">Last page</asp:LinkButton>
17 </div> Code in CS file:
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":
10 if (pageIndx > 0)
11 {
12 pageIndx -= 1;
13}
14 break;
15 case "next":
16 if (pageIndx < pages - 1)
17 {
18 pageIndx += 1;
19}
20 break;
21 case "last":
22 pageIndx = pages - 1;
23 break;
24default:
25 pageIndx = 0;
26 break;
27}
28 CurrentPage.Value = pageIndx.ToString();
29 NewsGrid.DataSource = NewsManager.GetNews(pageIndx, pageSize).Entities;
30 NewsGrid.DataBind();
31}
Isn’t it very simple? You will understand it after reading the code. I won’t go into detail here about using buttons to pass parameters. There is also the Entities attribute. In <<Application entity class EntitySet implements similar generic functions>> here can be found in articles
http://www.cnblogs.com/xdotnet/archive/2006/09/29/gridview_paging_myself.html