GridView自定义分页实例详解(附demo源码下载)

所属分类: 网络编程 / ASP.NET 阅读数: 1177
收藏 0 赞 0 分享

本文实例讲述了GridView自定义分页实现方法。分享给大家供大家参考,具体如下:

CSS样式

首先把CSS样式代码粘贴过来:

.gv
{
  border: 1px solid #D7D7D7;
  font-size:12px;
  text-align:center;
}
.gvHeader
{
  color: #3F6293;
  background-color: #F7F7F7;
  height: 24px;
  line-height: 24px;
  text-align: center;
  font-weight: normal;
  font-variant: normal;
}
.gvHeader th
{
  font-weight: normal;
  font-variant: normal;
}
.gvRow, .gvAlternatingRow, .gvEditRow
{
  line-height: 20px;
  text-align: center;
  padding: 2px;
  height: 20px;
}
.gvAlternatingRow
{
  background-color: #F5FBFF;
}
.gvEditRow
{
  background-color: #FAF9DD;
}
.gvEditRow input
{
  background-color: #FFFFFF;
  width: 80px;
}
.gvEditRow .gvOrderId input, .gvEditRow .gvOrderId
{
  width: 30px;
}
.gvEditRow .checkBox input, .gvEditRow .checkBox
{
  width: auto;
}
.gvCommandField
{
  text-align: center;
  width: 130px;
}
.gvLeftField
{
  text-align: left;
  padding-left: 10px;
}
.gvBtAField
{
  text-align: center;
  width: 130px;
}
.gvCommandField input
{
  background-image: url(../Images/gvCommandFieldABg.jpg);
  background-repeat: no-repeat;
  line-height: 23px;
  border-top-style: none;
  border-right-style: none;
  border-bottom-style: none;
  border-left-style: none;
  width: 50px;
  height: 23px;
  margin-right: 3px;
  margin-left: 3px;
  text-indent: 10px;
}
.gvPage
{
  padding-left: 15px;
  font-size: 18px;
  color: #333333;
  font-family: Arial, Helvetica, sans-serif;
}
.gvPage a
{
  display: block;
  text-decoration: none;
  padding-top: 2px;
  padding-right: 5px;
  padding-bottom: 2px;
  padding-left: 5px;
  border: 1px solid #FFFFFF;
  float: left;
  font-size: 12px;
  font-weight: normal;
}
.gvPage a:hover
{
  display: block;
  text-decoration: none;
  border: 1px solid #CCCCCC;
}

GridView样式

根据上面列出的CSS样式样式名称,将他们分别加入网页GridView的不同标记中,举例如下:

<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" CssClass="gvRow" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" CssClass="gvHeader" />
<AlternatingRowStyle BackColor="#F7F7F7" CssClass="gvAlternatingRow" />

Pager分页模板

其中gridview下方的换页代码为:

<PagerTemplate>
  <table width="100%" style="font-size:12px;">
    <tr>
    <td style="text-align: right">
      第<asp:Label ID="lblPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>'></asp:Label>页
      /共<asp:Label ID="lblPageCount" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount %>'></asp:Label>页&nbsp;&nbsp;
     <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首页</asp:LinkButton>
     <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton>
     <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton>
     <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页</asp:LinkButton>
     <asp:TextBox ID="txtNewPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' Width="20px" AutoPostBack="true" ></asp:TextBox>
     <asp:LinkButton ID="btnGo" runat="server" CommandArgument="GO" CommandName="Page" Text="GO" OnClick="btnGo_Click"></asp:LinkButton>
    </td>
    </tr>
  </table>
</PagerTemplate>

触发事件

方法btnGo_Click的定义如下所示:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
  GridView1.PageIndex = e.NewPageIndex;
  BindData();
}
protected void btnGo_Click(object sender, EventArgs e)
{
  if (((LinkButton)sender).CommandArgument.ToString().ToLower().Equals("go"))
  {
    GridViewRow gridViewRow = GridView1.BottomPagerRow;
    TextBox numBox = (TextBox)GridView1.BottomPagerRow.FindControl("txtNewPageIndex");
    int inputNum = Convert.ToInt32(numBox.Text);
    GridView1.PageIndex = inputNum - 1;
    BindData();
  }
}

效果图展示及源码下载

完整实例代码点击此处本站下载

希望本文所述对大家asp.net程序设计有所帮助。

更多精彩内容其他人还在看

ASP.NET Core Web App应用第三方Bootstrap模板的方法教程

这篇文章主要给大家介绍了关于ASP.NET Core Web App应用第三方Bootstrap模板的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起看看吧
收藏 0 赞 0 分享

如何给ASP.NET Core Web发布包做减法详解

在ASP.Net中可以使用打包与压缩这两种技术来提高Web应用程序页面加载的性能。下面这篇文章主要给大家介绍了关于如何给ASP.NET Core Web发布包做减法的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧
收藏 0 赞 0 分享

.net中捕捉全局未处理异常的三种方式示例

这篇文章主要给大家介绍了关于.net中捕捉全局未处理异常的三种方式,分别是Page_Error处理页面级未处理异常、通过HttpModule来捕获未处理的异常以及通过Global中捕获未处理的异常,需要的朋友可以参考下
收藏 0 赞 0 分享

.net中自定义错误页面的实现方法

这篇文章主要给大家介绍了关于.net中自定义错误页面实现的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

.net中自定义错误页面的实现升级篇

这篇文章主要给大家介绍了关于.net中自定义错误页面实现的相关资料,这篇文章是之前的升级篇,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

.Net Core学习教程之在Mvc中简单的使用日志组件

这篇文章主要给大家介绍了关于.Net Core学习教程之在Mvc中简单使用日志组件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

.NET发送邮件的实现方法示例

这篇文章主要给大家介绍了关于.NET发送邮件的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.net具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

把ASP.NET MVC项目部署到本地IIS上的完整步骤

最近会经常修改一些网站前端的内容,为了方便跟UI和产品交流,需要将自己修改过的页面及时发布到测试机或者是本地的IIS上。下面这篇文章主要给大家介绍了关于如何把ASP.NET MVC项目部署到本地IIS上的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

ASP.NET MVC中分部视图的应用方法

这篇文章主要给大家介绍了关于ASP.NET MVC中分部视图的应用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

VS2012实现简单登录界面

这篇文章主要为大家详细介绍了VS2012实现简单登录界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多