濮阳杆衣贸易有限公司

主頁 > 知識庫 > Aspnetpager對GridView分頁并順利導出Excel

Aspnetpager對GridView分頁并順利導出Excel

熱門標簽:地圖定位圖標標注 塔城代理外呼系統(tǒng) 天心智能電銷機器人 地圖標注的公司有哪些 濮陽外呼電銷系統(tǒng)怎么樣 代理接電話機器人如何取消 400電話辦理哪家性價比高 地圖標注專業(yè)團隊 遂寧市地圖標注app

一、前言

      談到分頁,在網(wǎng)頁上簡直到處都是。網(wǎng)絡的資源越來越多,如果不用分頁技術來顯示,就會拖拉很長很長。下面給大家分享分頁技術。

二、基本要點

      當要顯示數(shù)據(jù)量足夠大的時候,我們往往采用分頁顯示的處理辦法。分頁有真分頁和假分頁。

假分頁:從數(shù)據(jù)庫中取出所有的數(shù)據(jù),然后分頁在界面上顯示。訪問一次數(shù)據(jù)庫,但由于選擇的數(shù)據(jù)量比較大,所以第一次花費時間比較長,但之后每一頁的顯示都是直接、快速的,避免對數(shù)據(jù)庫的多次訪問。

真分頁:確定要顯示的數(shù)量和內(nèi)容,然后每次都去數(shù)據(jù)庫取出該少量數(shù)據(jù),優(yōu)點是數(shù)據(jù)量小,缺點是訪問數(shù)據(jù)庫頻繁。在大型網(wǎng)站中往往采用真分頁,比如百度的圖片獲取。

三、實例展示

      由于在ASP.NET中沒有Aspnetpager控件,需要自己添加,其實也非常簡單,下載的路徑:https://yunpan.cn/cPHWP3eEzgu7w 訪問密碼 99df。

      下載好后,添加對Aspnetpager.dll控件的引用,然后在工具箱→右擊→選擇項→找到Aspnetpager →確定。

圖一 添加引用

圖二 選擇項

圖三 添加工具

圖四 展示效果

前臺代碼:

%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AspNetPagerTest.aspx.cs" Inherits="test.AspNetPagerTest" %>

%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

!DOCTYPE html>

html xmlns="http://www.w3.org/1999/xhtml">
head runat="server">
meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 title>使用AspNetPager對GridView分頁/title>
 %--引用分頁控件的CSS--%>
 link href="css/Paging.css" rel="stylesheet" />
/head>
body>
 form id="form1" runat="server">
 div>
 %--gridview控件--%>
 asp:GridView ID="GridView1" runat="server" Width="100%" 
  CellPadding="4" ForeColor="#333333" GridLines="None">

  AlternatingRowStyle BackColor="White" />
  EditRowStyle BackColor="#2461BF" />
  FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  HeaderStyle BackColor="#507CD1" Font-Bold="True" 
  ForeColor="White" Height="25px" HorizontalAlign="Center" />
  PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  RowStyle BackColor="#EFF3FB" Height="20px" HorizontalAlign="Center" />
  SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
  SortedAscendingCellStyle BackColor="#F5F7FB" />
  SortedAscendingHeaderStyle BackColor="#6D95E1" />
  SortedDescendingCellStyle BackColor="#E9EBEF" />
  SortedDescendingHeaderStyle BackColor="#4870BE" />
 /asp:GridView>
 %--分頁控件--%>
 webdiyer:AspNetPager ID="AspNetPager1" runat="server" 
 onpagechanged="AspNetPager1_PageChanged" CssClass="anpager" 
 CurrentPageButtonClass="cpb" FirstPageText="首頁" LastPageText="尾頁" 
 NextPageText="后頁" PrevPageText="前頁" PageSize="5" HorizontalAlign="Center">
 /webdiyer:AspNetPager>

 br />
 %--導出按鈕--%>
 asp:Button ID="btnExcel" runat="server" OnClick="btnExcel_Click" Text="導出Excel" />
 br />
 br />
 br />
 br />

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

CSS代碼:

body { height: 382px;
}
.anpager 
{ 
 font: 11px Arial, Helvetica, sans-serif;
 padding:10px 20px 10px 0; 
 margin: 0px;
}
.anpager a 
{
 padding: 1px 6px; 
 border: solid 1px #ddd; 
 background: #fff; 
 text-decoration: none;
 margin-right:2px
}
.anpager a:visited 
{
 padding: 1px 6px; 
 border: solid 1px #ddd; 
 background: #fff; 
 text-decoration: none;
}
.anpager .cpb 
{
 padding: 1px 6px;
 font-weight: bold; 
 font-size: 13px;
 border:none
}
.anpager a:hover 
{
 color: #fff; 
 background: #ffa501;
 border-color:#ffa501;
 text-decoration: none;
}

后臺的代碼:

/*********************************************************************
 * 作者:王雷
 * 小組:暫無
 * 說明:【ASP.NET】Aspnetpager對GridView分頁,并導出Excel
 * 創(chuàng)建日期:2016年4月25日20:23:00
 * 版本號:V1.0.0
 ************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO; //導出Excel的時候用到
namespace test
{
 public partial class AspNetPagerTest : System.Web.UI.Page
 {

 public SqlConnection conn = null;
 public SqlCommand cmd = null;
 public SqlDataReader sdr = null;
 #region 界面加載--王雷--2016年4月25日20:21:29
 /// summary>
 /// 界面加載
 /// /summary>
 /// param name="sender">/param>
 /// param name="e">/param>
 protected void Page_Load(object sender, EventArgs e)
 {
  if (!IsPostBack)
  {
  //調(diào)用綁定分頁和GridView
  BindGridView();
  }
 }
 #endregion

 #region 綁定分頁和GridView方法--王雷--2016年4月25日20:20:59
 ///綁定分頁和GridView方法
 private void BindGridView()
 {

  //查詢語句
  string SQL = "select * from USERS"; 
  //獲取數(shù)據(jù)表格
  DataTable dt = ExecuteQuery(SQL, CommandType.Text);
  //初始化分頁數(shù)據(jù)源實例
  PagedDataSource pds = new PagedDataSource();
  //設置總行數(shù)
  AspNetPager1.RecordCount = dt.Rows.Count;
  //設置分頁的數(shù)據(jù)源
  pds.DataSource = dt.DefaultView;
  //設置當前頁
  pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
  //設置每頁顯示頁數(shù),在前臺界面中有設置
  pds.PageSize = AspNetPager1.PageSize;
  //啟用分頁
  pds.AllowPaging = true;
  //設置GridView的數(shù)據(jù)源為分頁數(shù)據(jù)源
  GridView1.DataSource = pds;
  //綁定GridView
  GridView1.DataBind();
 }
 #endregion 

 #region 執(zhí)行傳入的SQL查詢語句--王雷-2016年4月25日20:19:54
 ///summary >
 ///執(zhí)行傳入的SQL查詢語句
 /// /summary>
 /// param name="cmdText" >要執(zhí)行的SQL查詢語句或者是存儲過程/param>
 /// param name="ct">命令類型/param>
 /// returns >返回更新的記錄數(shù) /returns> 
 public DataTable ExecuteQuery(string cmdText, CommandType ct)
 {
  //建立數(shù)據(jù)連接字符串
  SqlConnection cnn = new SqlConnection("server=.;uid=sa;pwd=123456;database=Login");
  DataTable dt = new DataTable();
  cmd = new SqlCommand(cmdText, cnn);
  cmd.CommandType = ct;
  cnn.Open();
  using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) //關閉sdr的時候,也關閉連接。
  {
  dt.Load(sdr); //加載sdr,賦值給dt
  }
  cnn.Close();
  return dt;
 }
 #endregion 

 #region 分頁控件點擊頁面觸發(fā)改變事件,重新綁定數(shù)據(jù)源--王雷--2016年4月25日20:19:03
 /// summary>
 /// 分頁控件點擊頁面觸發(fā)改變事件,重新綁定數(shù)據(jù)源--王雷--2016年4月25日20:19:03
 /// /summary>
 /// param name="sender">/param>
 /// param name="e">/param>
 protected void AspNetPager1_PageChanged(object sender, EventArgs e)
 {
  //調(diào)用綁定分頁和GridView
  BindGridView();
 }
 #endregion

 #region 導出Excel的方法--王雷--2016年4月10日12:48:04
 /// summary>
 /// 導出Excel的方法
 /// /summary>
 /// param name="gv">/param>
 public void ExcelOut(GridView gv)
 {
  if (gv.Rows.Count > 0)
  {
  //attachment; filename =
  Response.Clear();
  Response.ClearContent();
  Response.AddHeader("Content-Disposition", "attachment; filename =志晟集團辦公用品申購單" + DateTime.Now.ToString("_yyyy/MM/dd") + ".xls");
  Response.ContentEncoding = System.Text.Encoding.UTF8;
  Response.ContentType = "application/ms-excel";
  StringWriter sw = new StringWriter();
  HtmlTextWriter htw = new HtmlTextWriter(sw);
  gv.RenderControl(htw);
  Response.Write(sw.ToString());
  Response.Flush();
  Response.End();

  }
  else
  {
  Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "script lang='javascript' defer >alert('沒有記錄');/script> ");
  }
 }
 #endregion

 #region 導出Excel--王雷
 /// summary>
 /// 導出Excel
 /// /summary>
 /// param name="sender">/param>
 /// param name="e">/param>
 protected void btnExcel_Click(object sender, EventArgs e)
 {
  ExcelOut(GridView1);
 }

 public override void VerifyRenderingInServerForm(Control control)
 {
  //base.VerifyRenderingInServerForm(control);
 }
 #endregion


 }
}


最后的效果圖:

圖五 效果圖

大家可能問,用這個控件來分頁,是真分頁呢?還是假分頁呢?

答案:真分頁。因為導出來的Excel只有本頁的,只能導出本頁索引到的頁面。

圖六 導出Excel

四、小結

      還是那句話,asp就是要多練,把一些經(jīng)常使用的技術,可以通過代碼庫來總計,以后用到的時候就可以搬走了~~加油!

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助。

您可能感興趣的文章:
  • 利用jQuery 實現(xiàn)GridView異步排序、分頁的代碼
  • jquery+ashx無刷新GridView數(shù)據(jù)顯示插件(實現(xiàn)分頁、排序、過濾功能)
  • asp.net中讓Repeater和GridView支持DataPager分頁
  • GridView高效分頁和搜索功能的實現(xiàn)代碼
  • AspNetPager+GridView實現(xiàn)分頁的實例代碼
  • Asp.net GridView使用大全(分頁實現(xiàn))
  • Android中實現(xiàn)多行、水平滾動的分頁的Gridview實例源碼
  • GridView分頁的實現(xiàn)以及自定義分頁樣式功能實例
  • asp.net gridview分頁:第一頁 下一頁 1 2 3 4 上一頁 最末頁

標簽:吉林 本溪 河南 宜春 重慶 麗江 汕頭 婁底

巨人網(wǎng)絡通訊聲明:本文標題《Aspnetpager對GridView分頁并順利導出Excel》,本文關鍵詞  Aspnetpager,對,GridView,分頁,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Aspnetpager對GridView分頁并順利導出Excel》相關的同類信息!
  • 本頁收集關于Aspnetpager對GridView分頁并順利導出Excel的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    洪江市| 江安县| 小金县| 栾城县| 长沙县| 碌曲县| 宣武区| 建阳市| 泰顺县| 黄浦区| 西青区| 息烽县| 咸宁市| 怀宁县| 武平县| 漳平市| 天津市| 仁寿县| 安新县| 五河县| 区。| 繁昌县| 石林| 南昌县| 南昌市| 忻城县| 永年县| 孙吴县| 富平县| 宜川县| 昌邑市| 太湖县| 乌拉特中旗| 东宁县| 嘉义市| 商城县| 曲沃县| 忻城县| 清水河县| 临高县| 大荔县|