1. aspx页面端代码:
Code
<div class="gv-footer">
<asp:LinkButton ID="btFirstPage" runat="server" CommandArgument="first" OnClick ="PagerButton_Click">首页</asp:LinkButton>
<asp:LinkButton ID="btPrevPage" runat="server" CommandArgument="prev" OnClick ="PagerButton_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="btNextPage" runat="server" CommandArgument="next" OnClick ="PagerButton_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="btLastPage" runat="server" CommandArgument="last" OnClick ="PagerButton_Click">尾页</asp:LinkButton>
页码:
<asp:Label ID="lbCurrentPage" runat="server" ForeColor="Blue"></asp:Label>
/
<asp:Label ID="lbTotalPage" runat="server"></asp:Label>
<asp:DropDownList ID="ddlPageSelect" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlPageSelect_SelectedIndexChanged"> </asp:DropDownList>
</div>
<div class="gv-footer">
<asp:LinkButton ID="btFirstPage" runat="server" CommandArgument="first" OnClick ="PagerButton_Click">首页</asp:LinkButton>
<asp:LinkButton ID="btPrevPage" runat="server" CommandArgument="prev" OnClick ="PagerButton_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="btNextPage" runat="server" CommandArgument="next" OnClick ="PagerButton_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="btLastPage" runat="server" CommandArgument="last" OnClick ="PagerButton_Click">尾页</asp:LinkButton>
页码:
<asp:Label ID="lbCurrentPage" runat="server" ForeColor="Blue"></asp:Label>
/
<asp:Label ID="lbTotalPage" runat="server"></asp:Label>
<asp:DropDownList ID="ddlPageSelect" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlPageSelect_SelectedIndexChanged"> </asp:DropDownList>
</div>
2. aspx.cs端代码:
Code
#region GridView分页
/// <summary>
/// 页面选择操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void PagerButton_Click(object sender, EventArgs e)
{
int pageIndex = gvSailingList.PageIndex;
string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();
switch (arg)
{
case "prev":
if (pageIndex > 0)
{
pageIndex -= 1;
}
break;
case "next":
if (pageIndex < gvSailingList.PageCount)
{
pageIndex += 1;
}
break;
case "last":
pageIndex = gvSailingList.PageCount - 1;
break;
default:
pageIndex = 0;
break;
}
gvSailingList.PageIndex = pageIndex;
BindSailing();
}
/// <summary>
/// 页码选择下拉条绑定
/// </summary>
protected void PageBind()
{
ddlPageSelect.Items.Clear();
for (int i = 0; i < gvSailingList.PageCount; i++)
{
ddlPageSelect.Items.Insert(i, Convert.ToString(i + 1));
}
if (gvSailingList.PageCount > 0)
{
SetPageButton(true);
ddlPageSelect.SelectedIndex = gvSailingList.PageIndex;
}
else
{
SetPageButton(false);
ddlPageSelect.SelectedIndex = -1;
}
}
/// <summary>
/// 设置页码按钮的可见性
/// </summary>
protected void SetPageButton(bool ViewEnable)
{
//无数据时按钮变灰
if (ViewEnable == false)
{
btFirstPage.Enabled = false;
btLastPage.Enabled = false;
btNextPage.Enabled = false;
btPrevPage.Enabled = false;
ddlPageSelect.Enabled = false;
}
//有数据时按钮显示
else if (ViewEnable == true)
{
btFirstPage.Enabled = true;
btLastPage.Enabled = true;
btNextPage.Enabled = true;
btPrevPage.Enabled = true;
ddlPageSelect.Enabled = true;
}
}
/// <summary>
/// 页码选择栏 显示控制
/// </summary>
protected void PageDisplay()
{
//页码显示
if (gvSailingList.PageCount > 0)
{
lbCurrentPage.Text = Convert.ToString(gvSailingList.PageIndex + 1);
}
else
{
lbCurrentPage.Text = Convert.ToString(gvSailingList.PageIndex);
}
lbTotalPage.Text = gvSailingList.PageCount.ToString();
}
/// <summary>
/// 跳转到指定页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlPageSelect_SelectedIndexChanged(object sender, EventArgs e)
{
gvSailingList.PageIndex = ddlPageSelect.SelectedIndex;
BindSailing();
}
#endregion
#region 私有方法
protected void BindSailing()
{
gvSailingList.DataSource = dt.DefaultView;
gvSailingList.DataBind();
PageBind();
PageDisplay();
}
#endregion
#region GridView分页
/// <summary>
/// 页面选择操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void PagerButton_Click(object sender, EventArgs e)
{
int pageIndex = gvSailingList.PageIndex;
string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();
switch (arg)
{
case "prev":
if (pageIndex > 0)
{
pageIndex -= 1;
}
break;
case "next":
if (pageIndex < gvSailingList.PageCount)
{
pageIndex += 1;
}
break;
case "last":
pageIndex = gvSailingList.PageCount - 1;
break;
default:
pageIndex = 0;
break;
}
gvSailingList.PageIndex = pageIndex;
BindSailing();
}
/// <summary>
/// 页码选择下拉条绑定
/// </summary>
protected void PageBind()
{
ddlPageSelect.Items.Clear();
for (int i = 0; i < gvSailingList.PageCount; i++)
{
ddlPageSelect.Items.Insert(i, Convert.ToString(i + 1));
}
if (gvSailingList.PageCount > 0)
{
SetPageButton(true);
ddlPageSelect.SelectedIndex = gvSailingList.PageIndex;
}
else
{
SetPageButton(false);
ddlPageSelect.SelectedIndex = -1;
}
}
/// <summary>
/// 设置页码按钮的可见性
/// </summary>
protected void SetPageButton(bool ViewEnable)
{
//无数据时按钮变灰
if (ViewEnable == false)
{
btFirstPage.Enabled = false;
btLastPage.Enabled = false;
btNextPage.Enabled = false;
btPrevPage.Enabled = false;
ddlPageSelect.Enabled = false;
}
//有数据时按钮显示
else if (ViewEnable == true)
{
btFirstPage.Enabled = true;
btLastPage.Enabled = true;
btNextPage.Enabled = true;
btPrevPage.Enabled = true;
ddlPageSelect.Enabled = true;
}
}
/// <summary>
/// 页码选择栏 显示控制
/// </summary>
protected void PageDisplay()
{
//页码显示
if (gvSailingList.PageCount > 0)
{
lbCurrentPage.Text = Convert.ToString(gvSailingList.PageIndex + 1);
}
else
{
lbCurrentPage.Text = Convert.ToString(gvSailingList.PageIndex);
}
lbTotalPage.Text = gvSailingList.PageCount.ToString();
}
/// <summary>
/// 跳转到指定页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlPageSelect_SelectedIndexChanged(object sender, EventArgs e)
{
gvSailingList.PageIndex = ddlPageSelect.SelectedIndex;
BindSailing();
}
#endregion
#region 私有方法
protected void BindSailing()
{
gvSailingList.DataSource = dt.DefaultView;
gvSailingList.DataBind();
PageBind();
PageDisplay();
}
#endregion
出处:wyforumid