您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)ASP.NETGridView的Bootstrap分頁樣式是怎樣的,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
Revenue.cs收入類,包括實(shí)體模型和業(yè)務(wù)邏輯
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內(nèi)容:
<%@ 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 > <h3 >ASP.NET GridView的Bootstrap分頁樣式</h3> <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
項(xiàng)目運(yùn)行結(jié)果如圖:
關(guān)于ASP.NETGridView的Bootstrap分頁樣式是怎樣的就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。