"ASP. Aspnetpager to GridView paging, and export Excel

Source: Internet
Author: User

First, preface

When it comes to paging, it's almost everywhere on the web. network resources are more and more, if not paged technology to display, it will be dragged for a long time. Below to share the paging technology.

Ii. Basic Points

When the amount of data to be displayed is large enough, we tend to use a pagination display approach. Pagination has true pagination and false pagination.

    • false Paging : Remove all data from the database and display it on the interface page. Access the database once, but because the amount of data selected is relatively large, the first time takes longer, but then each page is displayed directly and quickly, avoiding multiple accesses to the database.

    • true Paging : Determine the number and content to display, and then each time to go to the database to remove the small amount of data, the advantage is the small amount of data, the disadvantage is frequent access to the database. In large-scale Web sites are often used real paging, such as Baidu's image acquisition.

Third, the example shows

Since there is no aspnetpager control in ASP., it is very simple to add it yourself, the path to download: https://yunpan.cn/cPHWP3eEzgu7w access password 99df.

After downloading, add a reference to the Aspnetpager.dll control and then in the Toolbox → right click → select item → find aspnetpager→ OK.

                                                    图一 添加引用

                                                    图二 选择项

                                                    图三 添加工具

                                                    图四 展示效果

Front-Office Code:

<%@ Page language= "C #" autoeventwireup= "true" codebehind= "AspNetPagerTest.aspx.cs" inherits= "test. Aspnetpagertest "%><%@ Register assembly= "Aspnetpager" namespace= "Wuqi.webdiyer" tagprefix= " webdiyer"%><! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Server"><meta http-equiv="Content-type"Content="text/html; Charset=utf-8 "/> <title> css--%> <link href= using Aspnetpager to GridView paging </title> <%--Reference paging control"Css/paging.css"Rel="stylesheet"/>"Form1"runat="Server"> <div> <%--gridview controls--%> <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"/> "#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> <%--pagination control--%> <webdiyer:aspnetpager id="AspNetPager1"runat="Server"Onpagechanged="Aspnetpager1_pagechanged"cssclass="Anpager"currentpagebuttonclass="CPB"firstpagetext="Home"lastpagetext="Last"nextpagetext="Back Page"prevpagetext="front page"Pagesize="5"horizontalalign="Center"> </webdiyer:AspNetPager> <br/> <%--export button--%> <asp:button id="Btnexcel"runat="Server"onclick="Btnexcel_click"text="Export Excel"/> <br/> <br/> <br/> <br/> </div> </form>< /body>

CSS style:

Body {height:382px;}. Anpager {font: OnePX Arial, Helvetica, Sans-serif; PaddingTenPx -PxTenPx0; Margin0px;}. Anpager a {padding:1Px6px Border:solid1Px#ddd;Background#fff;Text-decoration:none; Margin-right:2Px}.anpager a:visited {padding:1Px6px Border:solid1Px#ddd;Background#fff;Text-decoration:none;}. Anpager. CPB {padding:1Px6px Font-weight:bold; Font-size: -px Border:none}.anpager a:hover {color:#fff;Background#ffa501;Border-color:#ffa501;Text-decoration:none;}

Code behind the scenes:

/********************************************************************* * Wang Lei * Group: No * Description: "ASP" Aspnetpager to the GridView page, and export Excel * Date created: .Years4Month -Day -: at:xx* Version number: 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; Use the namespace test{public partial when exporting Excel class aspnetpagertest :System.Web.UI.Page {public SqlConnection conn = null; public SqlCommand cmd = null; Public SqlDataReader SDR = null;#region Interface Loading--Wang Lei--April 25, 2016 20:21:29<summary>///interface load///</summary>//<param name="Sender"></param>//<param name="E"></param> protected void Page_Load (object sender, EventArgs e) {if(! IsPostBack) {//Invoke bound paging and GridView Bindgridview (); } }#endregion #region Bound Paging and GridView Method--Wang Lei--April 25, 2016 20:20:59Bind paging and GridView method private void Bindgridview () {//query statement string SQL ="SELECT * from USERS"; Get data table DataTable dt = ExecuteQuery (SQL, CommandType.Text); Initializes the paging data source instance PagedDataSource PDS = new PagedDataSource (); Set the total number of rows Aspnetpager1.recordcount = dt. Rows.Count; Sets the paging data source PDS. DataSource = dt. DefaultView; Sets the current page PDS. CurrentPageIndex = Aspnetpager1.currentpageindex-1; Set the number of pages per page, with the PDS set up in the foreground interface. PageSize = aspnetpager1.pagesize; Enable paging PDS. AllowPaging = true; Set the data source for the GridView as a paging data source Gridview1.datasource = PDS; Bind GridView Gridview1.databind (); }#endregion #region Execute an incoming SQL query statement--Wang Lei-April 25, 2016 20:19:54<summary >///execute Incoming SQL query statements///</summary>//<param name="Cmdtext"> SQL query statement to execute or stored procedure </param>//<param name="CT"> Command type </param>//<returns > Return updated record Count </returns> public DataTable ExecuteQuery (strin G cmdtext, CommandType ct) {//Establish data connection string 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))//When the SDR is closed, the connection is also closed. {dt. Load (SDR); Load SDR, assign to DT} CNN. Close ();returnDt }#endregion #region Pagination Control Click on the page to trigger the Change event, rebind the data source--Wang Lei--April 25, 2016 20:19:03<summary>///Pagination control Click Page Trigger Change event, rebind data Source--Wang Lei-- .Years4Month -Day -: +:Geneva</summary>//<param name="Sender"></param>//<param name="E"></param> protected void Aspnetpager1_pagechanged (object sender, EventArgs e) {//Call binding Paging and GridView Bindgridview (); }#endregion #region ways to export Excel--Wang Lei--April 10, 2016 12:48:04<summary>////Export Excel Method///</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 = Zhicheng Group Office supplies purchase Order "+ 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 (' No Records ');</script>"); } }#endregion #region Export excel--Wang Lei<summary>///export 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}}

The final:

 

You may ask, with this control to page, is true paging it? or a fake page break?

Answer: true pagination. Since Excel is the only one on this page, only the pages indexed on this page can be exported.

 图六 导出Excel
Iv. Summary

Or that sentence, the ASP is to practice more, some often use of technology, can be total through the code base, the later use of the time can be moved ~ ~ Refueling! Asp. NET really good fun, believe in yourself, you are the biggest!

"ASP. Aspnetpager to GridView paging, and export Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.