Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of GridView pagination and smooth Export of Excel by Aspnetpager

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the Aspnetpager of GridView paging and the smooth export of Excel example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.

I. Preface

When it comes to paging, it's all over the web page. There are more and more resources in the network, and if you don't use paging technology to display it, it will drag on for a long time. Next, I would like to share with you the paging technology.

II. Basic points

When the amount of data is large enough, we often use the method of paging display. There are true pages and false pages.

False paging: take all the data from the database and display it on the interface in paging. Visit the database once, but because of the large amount of data selected, it takes a long time for the first time, but then the display of each page is direct and fast, avoiding multiple visits to the database.

True paging: determine the quantity and content to be displayed, and then go to the database to retrieve the small amount of data every time. The advantage is that the amount of data is small, and the disadvantage is that the database is accessed frequently. True pagination is often used in large websites, such as Baidu's picture acquisition.

III. Demonstration of examples

Since there is no Aspnetpager control in ASP.NET, you need to add it yourself. in fact, it is very simple to download: https://yunpan.cn/cPHWP3eEzgu7w access password 99df.

After downloading, add a reference to the Aspnetpager.dll control, and then right-click the → selection → in the toolbox → to find the Aspnetpager → OK.

Figure 1 adding references

Figure 2 selection

Figure 3 adding tools

Figure 4 shows the effect

Foreground code:

Paging GridView using AspNetPager

CSS Code:

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;}

The code in the background:

/ * description: [ASP.NET] Aspnetpager paging GridView And export Excel * version number: V1.0.0 * * / using System Namespace test {public partial class AspNetPagerTest: System.Web.UI.Page {public SqlConnection conn = null; public SqlCommand cmd = null; public SqlDataReader sdr = null is used when 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; / / exports Excel # region interface loading-Wang Lei-20:21:29 on April 25, 2016 / interface loading / protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {/ / call binding paging and GridView BindGridView () } # endregion # region binds paging and GridView method-- Wang Lei-- 20:20:59 April 25, 2016 / / binds paging and GridView method private void BindGridView () {/ / query statement string SQL = "select * from USERS"; / / gets data table DataTable dt = ExecuteQuery (SQL, CommandType.Text); / / initializes paging data source instance PagedDataSource pds = new PagedDataSource () / / set the total number of rows AspNetPager1.RecordCount = dt.Rows.Count; / / set the data source of paging pds.DataSource = dt.DefaultView; / / set the current page pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex-1; / / set the number of pages displayed per page. In the foreground interface, set pds.PageSize = AspNetPager1.PageSize; / / enable paging pds.AllowPaging = true; / / set the data source of GridView as paging data source GridView1.DataSource = pds / / bind GridView GridView1.DataBind () } # endregion # region execute incoming SQL query statement-Wang Lei-20:19:54 on April 25, 2016 / execute incoming SQL query statement / SQL query statement to be executed or stored procedure / / command type / return the updated number of records public DataTable ExecuteQuery (string 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)) / / also closes the connection when sdr is closed. {dt.Load (sdr); / / load sdr and assign a value to dt} cnn.Close (); return dt } # endregion # region paging control clicks on the page to trigger the change event, and rebinds the data source-Wang Lei-20:19:03 on April 25, 2016 / the paging control clicks on the page to trigger the change event Rebind data source-Wang Lei-20:19:03 on April 25, 2016 / protected void AspNetPager1_PageChanged (object sender, EventArgs e) {/ / call bind paging and GridView BindGridView () } # endregion # region method for exporting Excel-- Wang Lei-- method for exporting Excel at 12:48:04 on April 10, 2016 / public void ExcelOut (GridView gv) {if (gv.Rows.Count > 0) {/ / attachment; filename = Response.Clear (); Response.ClearContent (); Response.AddHeader ("Content-Disposition", "attachment" Filename = Zhisheng Group Office supplies requisition form "+ 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", "alert ('no record');");}} # endregion # region Export Excel-- Wang Lei / Export Excel / protected void btnExcel_Click (object sender, EventArgs e) {ExcelOut (GridView1);} public override void VerifyRenderingInServerForm (Control control) {/ / base.VerifyRenderingInServerForm (control);} # endregion}}

The final effect picture:

Figure 5 effect diagram

You may ask, is it true to use this control for paging? Or fake pagination?

Answer: true pagination. Because the exported Excel is only for this page, only the pages to which this page is indexed can be exported.

Figure 6 Export Excel

Thank you for reading this article carefully. I hope the article "Aspnetpager paging GridView and successfully exporting Excel sample analysis" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report