gridview 分页(自定义)
(1) 在default.aspx 中
<PagerTemplate>
<table width="100%">
<tr><td style="text-align:right">
<asp:LinkButton ID="lkbFirst" runat="server" CommandArgument="First" CommandName="Page" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>" OnCommand="PageChange" text="首 页"></asp:LinkButton>
<asp:LinkButton ID="lkbPre" runat="server" CommandArgument="Pre" CommandName="Page" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>" OnCommand="PageChange" text="上一页" ></asp:LinkButton>
<asp:LinkButton ID="lkbNext" runat="server" CommandArgument="Next" CommandName="Page" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>" OnCommand="PageChange" text="下一页"></asp:LinkButton>
<asp:LinkButton ID="lkbLast" runat="server" CommandArgument="Last" CommandName="Page" Enabled="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>" OnCommand="PageChange" text="尾 页"></asp:LinkButton>
</span>
<%--
调转到 <asp:TextBox ID="tbxPage" runat="server" Width="20px"></asp:TextBox> 页
<asp:LinkButton ID="lkbGoPage" runat="server" CommandName="GoPage" OnCommand="GoToPage">GO</asp:LinkButton> --%>
第 <asp:Label ID="lblCurrent" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label> 页/
共 <asp:Label ID="lblTotal" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label> 页
</td></tr>
</table>
</PagerTemplate>
(2) C# 中增加
public void PageChange(object sender, System.Web.UI.WebControls.CommandEventArgs e)
{
int currentIndex = 0;
if (e.CommandArgument.ToString() == "First")
currentIndex = 0;
else if (e.CommandArgument.ToString() == "Pre")
currentIndex = GridView1.PageIndex > 0 ? GridView1.PageIndex - 1 : 0;
else if (e.CommandArgument.ToString() == "Next")
currentIndex = GridView1.PageIndex == GridView1.PageCount - 1 ? GridView1.PageIndex : GridView1.PageIndex + 1;
else if (e.CommandArgument.ToString() == "Last")
currentIndex = GridView1.PageCount - 1;
GridView1.PageIndex = currentIndex;
bind();
}
//指到指定的页数
//public void GoToPage(object sender, System.Web.UI.WebControls.CommandEventArgs e)
//{
// GridViewRow pagerRow = GridView1.BottomPagerRow; //控件中的底部页导航行
// TextBox tbxSearch = (TextBox)pagerRow.Cells[0].FindControl("tbxPage");
// int page = Convert.ToInt32(tbxSearch.Text.Trim()) - 1;
// if (tbxSearch !=null)
// {
// if (tbxSearch.Text.Trim() != "")
// page = int.Parse(tbxSearch.Text.Trim()) - 1;
// if (page < 0) { page = 0; }
// if (page > GridView1.PageCount)
// {
// page = GridView1.PageCount;
// }
// }
// GridView1.PageIndex = page;
// bind();
//}
public void bind()
{
GridView1.DataBind(); //Gridview的数据源绑定函数,自己写吧!
}