天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

如何在DataGrid控件中實(shí)現(xiàn)自定義分頁

如何在DataGrid控件中實(shí)現(xiàn)自定義分頁 
    在一般情況下,DataGrid控件每次實(shí)現(xiàn)翻頁操作時(shí),都會(huì)將數(shù)據(jù)源中的數(shù)據(jù)重新調(diào)用一次,當(dāng)數(shù)據(jù)中
數(shù)據(jù)很多時(shí),這樣做就會(huì)很浪費(fèi)系統(tǒng)資源和降低程序的執(zhí)行效率.這時(shí)候我們一般通過自定義分頁來解
決這個(gè)問題.
    DataGrid控件的AllowCustomPaging屬性用來獲取或設(shè)置DataGrid控件是否允許自定義分
頁;VirtualItemCoun屬性用來獲取或設(shè)置在使用自定義分頁時(shí)DataGrid中實(shí)際的項(xiàng)數(shù).要實(shí)現(xiàn)自定義分
頁,必須將AllowPaging與AllowCustomPaging屬性都設(shè)置為"True".
    在DataGrid中要實(shí)現(xiàn)自定義分頁的關(guān)鍵是,使該控件僅僅調(diào)用當(dāng)前顯示所需要的數(shù)據(jù)源數(shù)據(jù),在下
面的例子中通過CurrentPageIndex和PageSize屬性的值,在數(shù)據(jù)綁定時(shí)只取當(dāng)前頁需要的數(shù)據(jù).
    (1)頁面代碼:
復(fù)制代碼 代碼如下:
<%@ Page language="c#" Codebehind="Main.ASPx.cs" AutoEventWireup="false" 
Inherits="SissonDemo.Main" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>Main</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" 
content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <FONT face="宋體">
                <ASP:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: 
absolute; TOP: 24px" runat="server"
                    Width="792px" Height="96px" AllowCustomPaging="True" 
AllowPaging="True" PageSize="5">
                    <PagerStyle Mode="NumericPages"></PagerStyle>
                </ASP:DataGrid></FONT>
        </form>
    </body>
</HTML>
    (2)后臺(tái)代碼:
復(fù)制代碼 代碼如下:
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;

namespace SissonDemo
{
    /**//// <summary>
    /// Main 的摘要說明。
    /// </summary>
    public class Main : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
         int startIndex = 0;//用來保存當(dāng)前頁數(shù)據(jù)項(xiàng)的起始索引
        private void Page_Load(object sender, System.EventArgs e)
        {
            if(!IsPostBack)
            {
                Bind();//初始化時(shí)進(jìn)行數(shù)據(jù)綁定
            }

        }
        void Bind()//綁定數(shù)據(jù)方法
        {  //定義數(shù)據(jù)庫連接對(duì)象
            SqlConnection cn=new SqlConnection("server=.;database=pubs;uid=sa;pwd=");
           //創(chuàng)建數(shù)據(jù)適配對(duì)象
            SqlDataAdapter da=new SqlDataAdapter("select title_id ,title ,type, pub_id 
,price,pubdate from titles",cn);
            //創(chuàng)建DataSet對(duì)象
            DataSet ds=new DataSet();
            try
            {   //從指定的索引開始取PageSize條記錄.
                da.Fill(ds,startIndex,DataGrid1.PageSize,"CurDataTable");
                da.Fill(ds,"AllDataTable");//填充數(shù)據(jù)集合
                //設(shè)置DataGrid控件實(shí)際要顯示的項(xiàng)數(shù)
                DataGrid1.VirtualItemCount=ds.Tables["AllDataTable"].Rows.Count;
                //進(jìn)行數(shù)據(jù)綁定
                DataGrid1.DataSource=ds.Tables["CurDataTable"];
                DataGrid1.DataBind();

            }
            catch
            {
                Page.RegisterClientScriptBlock("","<script>alert('數(shù)據(jù)顯示錯(cuò)
誤');</script>");
            }

        }

        Web 窗體設(shè)計(jì)器生成的代碼#region Web 窗體設(shè)計(jì)器生成的代碼
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計(jì)器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /**//// <summary>
        /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
        /// 此方法的內(nèi)容。
        /// </summary>
        private void InitializeComponent()
        {    
            this.DataGrid1.PageIndexChanged += new 
System.Web.UI.WebControls.DataGridPageChangedEventHandler
(this.DataGrid1_PageIndexChanged);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void DataGrid1_PageIndexChanged(object source, 
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
        {
                //設(shè)置DataGrid當(dāng)前頁的索引值為用戶選擇的頁的索引
                DataGrid1.CurrentPageIndex=e.NewPageIndex;
                //取得當(dāng)前頁為止總共有多少條記錄,以便在就從該記錄開始讀取
                startIndex=DataGrid1.PageSize*DataGrid1.CurrentPageIndex;
               //取得綁定數(shù)據(jù)
                Bind();
        }
    }
}

    在這段程序中,首先在數(shù)據(jù)綁定時(shí),設(shè)置DataGrid控件的VirtualItemCoun屬性值為查詢結(jié)果集中的
記錄總數(shù),然后取得當(dāng)前頁要顯示的數(shù)據(jù),初始化時(shí)當(dāng)前頁顯示的數(shù)據(jù)為從取到的數(shù)據(jù)的零位置開始,到
DataGrid控件的PageSize屬性的設(shè)定值為止的數(shù)據(jù)記錄數(shù).在分頁操作中重新定義了取得數(shù)據(jù)時(shí)
的數(shù)據(jù)項(xiàng)的開始索引值,然后調(diào)用數(shù)據(jù)綁定方法把取到的新數(shù)據(jù)和DataGrid控件重新綁定. 

AspNet技術(shù)如何在DataGrid控件中實(shí)現(xiàn)自定義分頁,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产日韩久久久精品影院首页 | 青草视频久久 | 99久久99久久精品 | 野花韩国在线观看 | 久久这里只精品热在线99 | 东北小伙FREECHINESE野外 东北嫖妓对白粗口 | 久久久久国产精品美女毛片 | 亚洲国产三级在线观看 | 国产亚洲色婷婷久久精品99 | 三级在线网址 | 91免费精品国自产拍在线可以看 | xxxx免费观看 | 久久免费精品国产72精品剧情 | 性与肉体电影免费观看 | 亚洲国产高清在线观看视频 | 国产欧美日韩国产高清 | 耻辱诊察室1一4集动漫在线观看 | 中文字幕不卡免费高清视频 | 扒开美女下面粉嫩粉嫩冒白浆 | 四虎亚洲中文字幕永久在线 | 北条麻妃夫の友人196 | 日日夜夜影院在线播放 | 精品久久久亚洲精品中文字幕 | 琪琪午夜福利免费院 | 色综合伊人色综合网站下载 | 秋霞电影网午夜鲁丝片无码 | 99精品视频在线观看免费播放 | 99久久国产宗和精品1上映 | 国产成人精品区在线观看 | 红豆视频免费资源观看 | 国产在线观看码高清视频 | 西西人体一级裸片 | 最新2017年韩国伦理片在线 | 久99久热只有精品国产99 | 无码日韩人妻精品久久蜜桃入口 | 一本道手机无码在线看 | 日韩1区1区产品乱码芒果榴莲 | 日韩欧美视频一区二区在线观看 | 海角社区在线视频播放观看 | 久久久大香菇 | 99久久精品久久久 |