ASP.NET GridView的Bootstrap分页样式_实用技巧

本文实例为大家分享了GridView的Bootstrap分页样式,供大家参考,具体内容如下

Revenue.cs收入类,包括实体模型和业务逻辑

 public class Revenue
 {

 public Revenue(string country, string revenue, string salesmanager, string year)
 {
  this.country = country;
  this.revenue = revenue;
  this.salesmanager = salesmanager;
  this.year = year;
 }

 public Revenue() { }

 public string country { get; set; }
 public string revenue { get; set; }
 public string salesmanager { get; set; }
 public string year { get; set; }

 public List<Revenue> GetRevenueDetails(int pagenumber,int maxrecords)
 {
  List<Revenue> lstRevenue = new List<Revenue>();
  string filename = HttpContext.Current.Server.MapPath("~/App_Data/country_revenue.csv");
  int startrecord = (pagenumber * maxrecords) - maxrecords;
  if (File.Exists(filename))
  {
  IEnumerable<int> range = Enumerable.Range(startrecord, maxrecords);
  IEnumerable<String> lines = getFileLines(filename, range);
  foreach (String line in lines)
  {
   string[] row = line.Split(',');
   lstRevenue.Add(new Revenue(row[0], row[1], row[2], row[3]));
  }

  }
  return lstRevenue;
 }

 public static IEnumerable<String> getFileLines(String path, IEnumerable<int> lineIndices)
 {
  return File.ReadLines(path).Where((l, i) => lineIndices.Contains(i));
 }

 public int GetTotalRecordCount()
 {
  string filename = HttpContext.Current.Server.MapPath("~/App_Data/country_revenue.csv");
  int count = 0;
  if (File.Exists(filename))
  {
  string[] data = File.ReadAllLines(filename);
  count= data.Length;
  }
  return count;
 }
 }

Default.aspx内容:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridViewBootstrapPagination.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title>GridView的Bootstrap分页样式</title>
 <link href="Styles/bootstrap.min.css" rel="stylesheet" />
 <script src="Scripts/jquery-1.8.2.js"></script>
 <script src="Scripts/jquery.bootpag.min.js"></script>
 <script type="text/javascript">
 $(document).ready(function () {
  // init bootpag
  var count = GetTotalPageCount();
  $('#page-selection').bootpag(
  {
   total:count
  }).on("page", function (event, num) {
   GetGridData(num);
  });
 });

 function GetGridData(num) {

  $.ajax({
  type: "POST",
  url: "Default.aspx/GetRevenueDetail",
  data: "{ \"pagenumber\":" + num + "}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function (data) {
   bindGrid(data.d);
  },
  error: function (xhr, status, err) {
   var err = eval("(" + xhr.responseText + ")");
   alert(err.Message);

  }
  });
 }

 function bindGrid(data) {
  $("[id*=gvBSPagination] tr").not(":first").not(":last").remove();
  var table1 = $('[id*=gvBSPagination]');
  var firstRow = "$('[id*=gvBSPagination] tr:first-child')";
  for (var i = 0; i < data.length; i++) {

  var rowNew = $("<tr><td></td><td></td><td></td><td></td></tr>");
  rowNew.children().eq(0).text(data[i].country);
  rowNew.children().eq(1).text(data[i].revenue);
  rowNew.children().eq(2).text(data[i].salesmanager);
  rowNew.children().eq(3).text(data[i].year);
  rowNew.insertBefore($("[id*=gvBSPagination] tr:last-child"));
  }
 }

 function GetTotalPageCount() {
  var mytempvar = 0;
  $.ajax({
  type: "POST",
  url: "Default.aspx/GetTotalPageCount",
  data: "",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  async:false,
  success: function (data) {
   mytempvar=data.d;

  },
  error: function (xhr, status, err) {
   var err = eval("(" + xhr.responseText + ")");
   alert(err.Message);

  }
  });
  return mytempvar;
 }

 </script>
</head>
<body>
 <form id="form1" runat="server">
 <div style="width:670px;margin-left:auto;margin-right:auto;">
 <h2 style="text-align:center;">ASP.NET GridView的Bootstrap分页样式</h2>
 <asp:GridView ID="gvBSPagination" runat="server" CssClass="table table-striped table-bordered table-condensed" Width="660px" AllowPaging="true" PageSize="5" OnPreRender="gvBSPagination_PreRender">
  <PagerTemplate>
  <div id="page-selection" class="pagination-centered"></div>
  </PagerTemplate>
 </asp:GridView>
 <div id="content"></div> 

 </div>
 </form>
</body>
</html>

后台代码:

 public partial class Default : System.Web.UI.Page
 {
 private const int MAX_RECORDS = 5;

 protected void Page_Load(object sender, EventArgs e)
 {
  string filename = Server.MapPath("~/App_Data/country_revenue.csv");
  if (!IsPostBack)
  {
  List<Revenue> revenue = GetRevenueDetail(1);
  gvBSPagination.DataSource = revenue;
  gvBSPagination.DataBind();

  }

 }

 [WebMethod]
 [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
  public static List<Revenue> GetRevenueDetail(int pagenumber)
  {
  Revenue rv = new Revenue();
  List<Revenue> lstrevenue = rv.GetRevenueDetails(pagenumber,MAX_RECORDS);
  return lstrevenue;
 }

 [WebMethod]
 [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
 public static int GetTotalPageCount()
 {
  int count=0;
  Revenue rv=new Revenue();
  count = rv.GetTotalRecordCount();
  count = count / MAX_RECORDS;
  return count;
 }
 protected void gvBSPagination_PreRender(object sender, EventArgs e)
 {
  GridView gv = (GridView)sender;
  GridViewRow pagerRow = (GridViewRow)gv.BottomPagerRow;

  if (pagerRow != null && pagerRow.Visible == false)
  pagerRow.Visible = true;
 }
 }

country_revenue.csv

项目运行结果如图:

如果大家还想深入学习,可以点击这里进行学习,再为大家附3个精彩的专题:

Bootstrap学习教程

Bootstrap实战教程

Bootstrap插件使用教程

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索bootstrap
, asp.net
, gridview
分页
,以便于您获取更多的相关知识。

时间: 2024-11-03 20:05:45

ASP.NET GridView的Bootstrap分页样式_实用技巧的相关文章

Asp.net GridView使用大全(分页实现)_实用技巧

GridView自带的分页功能实现: 要实现GrdView分页的功能操作如下:1.更改GrdView控件的AllowPaging属性为true.2.更改GrdView控件的PageSize属性为 任意数值(默认为10)3.更改GrdView控件的PageSetting->Mode为Numeric等(默认为Numeric)该属性为分页样式.GridView属性设置好了,从页面上也能看到分页样式. 现在开始实现分页的功能: 1.在<<asp:GridView ID=......>后添加

Asp.Net实现的通用分页函数_实用技巧

本文实例讲述了Asp.Net实现的通用分页函数.分享给大家供大家参考,具体如下: 功能: 1.每页设置显示9页,超过9页,点5页后的+1页显示(可以随便修改) 2.CSS样式自己可以设置 3.无任何咋代码产生,利于搜索引擎优化 分页程序 objPDS = new PagedDataSource(); objPDS.DataSource = dtTable.DefaultView;//绑定数据源 objPDS.AllowPaging = true; objPDS.PageSize =10;//分页

asp.net控件DataList分页用法_实用技巧

本文实例讲述了asp.net控件DataList分页用法.分享给大家供大家参考,具体如下: protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState["Page"] = 0; Bangding(); } } //绑定数据 public void Bangding() { PagedDataSource pds = new PagedDataSource(); pds.DataS

asp.net Linq TO Sql 分页方法_实用技巧

分页方法 复制代码 代码如下: /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="replist">控件ID</param> /// <param name="DataSource">数据源</param> /// <par

asp.net GridView导出到Excel代码_实用技巧

复制代码 代码如下: StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); HtmlTextWriter htw = new HtmlTextWriter(sw); Page page = new Page(); HtmlForm form = new HtmlForm(); GVLinkman.EnableViewState = false; page.EnableEventValidat

asp.net Gridview里添加汇总行_实用技巧

if (e.Row.RowType == DataControlRowType.DataRow) { totalcash += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "cash")); totalunbalanced += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "unbalanced")); totalsettled += Convert.To

GridView分页的实现(通用分页模板)_实用技巧

要在GridView中加入 //实现分页 AllowPaging="true" //一页数据10行 PageSize="10" // 分页时触发的事件 OnPageIndexChanging="gvwDesignationName_PageIndexChanging" 在服务器事件里 复制代码 代码如下: protectedvoid gvwDesignationName_PageIndexChanging(object sender, GridV

C#精髓 GridView72大绝技 学习gridview的朋友必看_实用技巧

快速预览:GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠标移到GridView某一行时改变该行的背景色方法一鼠标移到GridView某一行时改变该行的背景色方法二GridView实现删除时弹出确认对话框GridView实现自动编号GridView实现自定义时间货币等字符串格式GridView实现用"..."代替超长字符串GridView一般换行

NET Core TagHelper实现分页标签_实用技巧

这里将要和大家分享的是学习总结使用TagHelper实现分页标签,之前分享过一篇使用HtmlHelper扩展了一个分页写法地址可以点击这里http://www.jb51.net/article/89272.htm,今天分享的是net core的另外一种能写分页标签的方法具体是继承TagHelper,如下将讲述实现一个简单分页和总要注意步奏. 1.继承父类TagHelper并重写Process方法(这里还有一个异步的方法ProcessAsync各位可以自己尝试下) 2.注意:怎么在试图页面使用自定