国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
為datagrid的自帶分頁添加首頁、尾頁及狀態(tài)功能
DataGrid提供了分頁功能,不過看上去功能有限,但是我們可以通過DataGrid的一些屬性來獲取狀態(tài)以及增加首頁、尾頁功能按鈕。這里沒有使用DataGrid的自定義分頁功能,如果在速度效率不是很講究的情況下,由DataGrid自己管理分頁還是不錯的,付出的代價就是要把整個相關(guān)數(shù)據(jù)取出來后再刪選指定頁的數(shù)據(jù)。好處就是開發(fā)速度快,不需要寫分頁的存儲過程。本文事例使用的是Sql Server中的Northwind數(shù)據(jù)庫。運行界面如下:
對于前臺的顯示界面,我放了一個DataGrid;四個LinkButton導向按鈕;四個Literal來顯示紀錄狀態(tài)。
剩下的就是用表格定位。
這里需要設(shè)置DataGrid的AllowPaging屬性為True,同時設(shè)置AllowCustomPaging屬性位false(默認為false),設(shè)置PagerStyle的Visible屬性為False,使前臺不顯示。
前臺的代碼如下:
<%@ Page language="c#" Codebehind="DataGridPaging.aspx.cs" AutoEventWireup="false" Inherits="ZZ.AspnetPaging.DataGridPaging" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
     <HEAD>
         <title>DataGridPaging</title>
         <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
         <meta content="C#" name="CODE_LANGUAGE">
         <meta content="javaScript" name="vs_defaultClientScript">
         <meta content="     </HEAD>
     <body>
         <form id="Form1" method="post" runat="server">
              <TABLE id="Table1" style="FONT-SIZE: 9pt" cellSpacing="1" cellPadding="1" width="450" align="center"
                   border="1">
                   <TR>
                       <TD><asp:datagrid id="DataGrid1" runat="server" PageSize="5" Width="100%" AllowPaging="True">
                                 <HeaderStyle Font-Size="9pt"></HeaderStyle>
                                 <FooterStyle Font-Size="9pt"></FooterStyle>
                                 <PagerStyle Visible="False" Font-Size="9pt" Mode="NumericPages"></PagerStyle>
                            </asp:datagrid></TD>
                   </TR>
              </TABLE>
              <TABLE id="Table2" style="FONT-SIZE: 9pt" cellSpacing="1" cellPadding="1" width="450" align="center"
                   border="1">
                   <TR>
                       <TD style="WIDTH: 207px">
                            <asp:linkbutton id="LBtnFirst" runat="server" CommandName="First">首頁</asp:linkbutton>
                            <asp:linkbutton id="LBtnPRev" runat="server" CommandName="Prev">上一頁</asp:linkbutton>
                            <asp:linkbutton id="LBtnNext" runat="server" CommandName="Next">下一頁</asp:linkbutton>
                            <asp:linkbutton id="LBtnLast" runat="server" CommandName="Last">尾頁</asp:linkbutton> </TD>
                       <TD>第
                            <asp:literal id="LtlPageIndex" runat="server"></asp:literal>頁 共
                            <asp:literal id="LtlPageCount" runat="server"></asp:literal>頁 每頁
                            <asp:literal id="LtlPageSize" runat="server"></asp:literal>條 共
                            <asp:literal id="LtlRecordCount" runat="server"></asp:literal>條
                       </TD>
                   </TR>
              </TABLE>
         </form>
     </body>
</HTML>
后臺cs文件代碼,DataGridPaging類從System.Web.UI.Page繼承,在數(shù)據(jù)綁定時需要注意沒有數(shù)據(jù)的情況(0頁時),以及當頁數(shù)減少時避免前臺正在反頁導致缺頁。
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.sessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
 
namespace ZZ.AspnetPaging
{
     public class DataGridPaging : System.Web.UI.Page
     {
         private static string connString = ConfigurationSettings.AppSettings["ConnString"];
         private int recordCount;
         private int pageCount;
 
         protected System.Web.UI.WebControls.LinkButton LBtnFirst;
         protected System.Web.UI.WebControls.LinkButton LBtnPrev;
         protected System.Web.UI.WebControls.LinkButton LBtnNext;
         protected System.Web.UI.WebControls.LinkButton LBtnLast;
          protected System.Web.UI.WebControls.Literal LtlPageIndex;
         protected System.Web.UI.WebControls.Literal LtlPageCount;
         protected System.Web.UI.WebControls.Literal LtlPageSize;
         protected System.Web.UI.WebControls.Literal LtlRecordCount;
         protected System.Web.UI.WebControls.DataGrid DataGrid1;
    
         private void Page_Load(object sender, System.EventArgs e)
         {
              if(!Page.IsPostBack)
              {
                   DataGridDataBind();
              }
         }
 
         //綁定數(shù)據(jù)
         private void DataGridDataBind()
         {
              DataSet ds = GetCustomersData();
              recordCount = ds.Tables[0].Rows.Count;
              //獲取當前的頁數(shù)
              pageCount = (int)Math.Ceiling( recordCount * 1.0 / PageSize);
              //避免紀錄從有到無時,并且已經(jīng)進行過反頁的情況下CurrentPageIndex > PageCount出錯
              if(recordCount ==0)
              {
                   this.DataGrid1.CurrentPageIndex = 0;
              }
              else if(this.DataGrid1.CurrentPageIndex >= pageCount)
              {
                   this.DataGrid1.CurrentPageIndex = pageCount - 1;
              }
              this.DataGrid1.DataSource = ds;
              this.DataGrid1.DataBind();
              NavigationStateChange();
         }
 
         #region Web 窗體設(shè)計器生成的代碼
         override protected void OnInit(EventArgs e)
         {
              //
              // CODEGEN: 該調(diào)用是 asp.net Web 窗體設(shè)計器所必需的。
              //
              InitializeComponent();
              base.OnInit(e);
         }
        
         ///<summary>
         ///設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
         ///此方法的內(nèi)容。
         ///</summary>
         private void InitializeComponent()
         {   
              this.LBtnFirst.Click += new System.EventHandler(this.LBtnNavigation_Click);
              this.LBtnPrev.Click += new System.EventHandler(this.LBtnNavigation_Click);
              this.LBtnNext.Click += new System.EventHandler(this.LBtnNavigation_Click);
              this.LBtnLast.Click += new System.EventHandler(this.LBtnNavigation_Click);
              this.Load += new System.EventHandler(this.Page_Load);
 
         }
         #endregion
 
         private void LBtnNavigation_Click(object sender, System.EventArgs e)
         {
              LinkButton btn = (LinkButton)sender;
              switch(btn.CommandName)
              {
                   case "First":
                       PageIndex = 0;
                       break;
                   case "Prev"://if( PageIndex > 0 )
                            PageIndex = PageIndex - 1;
                       break;
                   case "Next"://if( PageIndex < PageCount -1)
                            PageIndex = PageIndex + 1;
                       break;
                   case "Last":
                       PageIndex = PageCount - 1;
                       break;
              }
              DataGridDataBind();             
         }
         //數(shù)據(jù)綁定
         public static DataSet GetCustomersData()
         {
              SqlConnection conn = new SqlConnection(connString);
              string sqlStr = "SELECT CustomerID, CompanyName,Address,Phone FROM Customers";
              SqlCommand comm = new SqlCommand( sqlStr ,conn);
              SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
              DataSet ds = new DataSet();
              dataAdapter.Fill(ds);
              return ds;
         }
 
         ///<summary>
         ///控制導航按鈕或數(shù)字的狀態(tài)
         ///</summary>
         public void NavigationStateChange()
         {
              if( PageCount <= 1 )//( RecordCount <= PageSize )//小于等于一頁
              {
                   this.LBtnFirst.Enabled = false;
                   this.LBtnPrev.Enabled = false;
                   this.LBtnNext.Enabled = false;
                   this.LBtnLast.Enabled = false;
              }
              else //有多頁
              {
                   if( PageIndex == 0 )//當前為第一頁
                   {
                       this.LBtnFirst.Enabled = false;
                       this.LBtnPrev.Enabled = false;
                       this.LBtnNext.Enabled = true;
                       this.LBtnLast.Enabled = true;
                                    
                   }
                   else if( PageIndex == PageCount - 1 )//當前為最后頁
                   {
                       this.LBtnFirst.Enabled = true;
                       this.LBtnPrev.Enabled = true;
                       this.LBtnNext.Enabled = false;
                       this.LBtnLast.Enabled = false;
                                    
                   }
                   else //中間頁
                   {
                       this.LBtnFirst.Enabled = true;
                       this.LBtnPrev.Enabled = true;
                       this.LBtnNext.Enabled = true;
                       this.LBtnLast.Enabled = true;
                   }
                  
              }
              if(RecordCount == 0)//當沒有紀錄時DataGrid.PageCount會顯示1頁
                   this.LtlPageCount.Text = "0";
              else
                   this.LtlPageCount.Text = PageCount.ToString();
              if(RecordCount == 0)
                   this.LtlPageIndex.Text = "0";
              else
                   this.LtlPageIndex.Text = (PageIndex + 1).ToString();//在有頁數(shù)的情況下前臺顯示頁數(shù)加1
              this.LtlPageSize.Text = PageSize.ToString();
              this.LtlRecordCount.Text = RecordCount.ToString();
         }
 
        
         // 總頁數(shù)
         public int PageCount
         {
              get{return this.DataGrid1.PageCount;}
         }
         //頁大小
         public int PageSize
         {
              get{return this.DataGrid1.PageSize;}
         }
         //頁索引,從零開始
         public int PageIndex
         {
              get{return this.DataGrid1.CurrentPageIndex;}
              set{this.DataGrid1.CurrentPageIndex = value;}
         }
         // 紀錄總數(shù)
         public int RecordCount
         {
              get{return recordCount;}
              set{recordCount = value;}
         }
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Asp.net中DataGrid控件的自定義分頁
GridView的分頁-萬能分頁代碼
GrilView漂亮分頁效果
access下的分頁方案
gridview分頁模型
最高效的分頁存儲過程
更多類似文章 >>
生活服務(wù)
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服