小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

DataGrid終極分頁法:給分頁加上總記錄數(shù)、總頁數(shù)、當(dāng)前頁數(shù)

 昵稱2807 2007-04-29
DataGrid終極分頁法:給分頁加上總記錄數(shù)、總頁數(shù)、當(dāng)前頁數(shù)
 如何給DataGrid加上總記錄數(shù)、總頁數(shù)、當(dāng)前頁數(shù)?如下圖所示:

查看更多精彩圖片

    還是在DataGridPage.aspx.cs頁面里,找到DataGrid1_ItemCreated事件,在這個事件的最后,加上以下代碼:

Label la = new Label();
la.Text = "<table width=100%><tr><td align=left>總記錄數(shù):<b>"+ViewState["Row"].ToString()+" </b>";
la.Text += "總頁數(shù):<b>"+this.DataGrid1.PageCount+" </b>";
la.Text += "當(dāng)前頁:<font color=red><b>"+(this.DataGrid1.CurrentPageIndex+1).ToString()+" </b></font></td><td align=right>";
pager.Controls.AddAt(0,la);

Label lb = new Label();
lb.Text = "</td></tr></table>";
pager.Controls.Add(lb);

    然后在DataGridBind()的“DataView dv = ds.Tables[0].DefaultView;”后加上一句“ViewState["Row"] = dv.Count;”

    編譯后看看效果吧。下列是完整的頁面代碼:

DataGridPage.aspx:

<%@ Page language="c#" Codebehind="DataGridPage.aspx.cs" AutoEventWireup="false" Inherits="test.DataGridPage" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

     <HEAD>

         <title>DataGridPage</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">

         <style type="text/css"> BODY { FONT-SIZE: 9pt; BACKGROUND-COLOR: #ffffff } A:visited { COLOR: #000000; TEXT-DECORATION: none } A:active { COLOR: #000000; TEXT-DECORATION: none } A:hover { LEFT: 1px; COLOR: #000000; POSITION: relative; TOP: 1px; TEXT-DECORATION: underline } A:link { COLOR: #000000; TEXT-DECORATION: none } TD { FONT-SIZE: 9pt; FONT-FAMILY: "宋體" } </style>

     </HEAD>

     <body>

         <form id="Form1" method="post" runat="server">

              <FONT face="宋體">

                   <asp:DataGrid id="DataGrid1" runat="server" AllowPaging="True">

                       <PagerStyle VerticalAlign="Middle" HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>

                   </asp:DataGrid></FONT>

         </form>

     </body>

</HTML>

 

DataGridPage.aspx.cs:

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.OleDb;

 

namespace test

{

     /// <summary>

     /// DataGridPage 的摘要說明。

     /// </summary>

     public class DataGridPage : System.Web.UI.Page

     {

         protected System.Web.UI.WebControls.DataGrid DataGrid1;

         protected OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=C:\\Program Files\\Microsoft Office\\OFFICE11\\SAMPLES\\Northwind.mdb");

    

         private void Page_Load(object sender, System.EventArgs e)

         {

              if (!this.IsPostBack)

              {

                   DataGridBind();

                  

              }

         }

 

         private void DataGridBind()

         {

              string strSql = "select * from 產(chǎn)品";

              DataSet ds = new DataSet();

              conn.Open();

              OleDbDataAdapter myAdapter =  new OleDbDataAdapter(strSql,conn);

              myAdapter.Fill(ds,"ds");

              conn.Close();

 

              DataView dv = ds.Tables[0].DefaultView;

              ViewState["Row"] = dv.Count;;

              this.DataGrid1.DataSource = dv;

              this.DataGrid1.DataBind();

         }

 

         #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.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);

              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)

         {

              this.DataGrid1.CurrentPageIndex = e.NewPageIndex;

              DataGridBind();

         }

 

         private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

         {

              ListItemType elemType = e.Item.ItemType;

 

              if (elemType == ListItemType.Pager)

              {

                   TableCell pager = (TableCell)e.Item.Controls[0];

                   int up = 0;

                   int down = 0;

                   for (int i=0;i<pager.Controls.Count;i+=2)

                   {

                       Object o = pager.Controls[i];

                       if (o is LinkButton)

                       {

                            LinkButton h = (LinkButton) o;

                            if (h.Text!="...")

                            {

                                 h.ToolTip = "跳轉(zhuǎn)到第"+h.Text+"";

                            }

                            if (i==2)

                            {

                                 up = int.Parse(h.Text)-1;

                            }

                            if (i==pager.Controls.Count-3)

                            {

                                 down = int.Parse(h.Text)+1;

                            }

                       }

                       else

                       {

                            Label l = (Label) o;

 

                            if (i==2)

                            {

                                 up = int.Parse(l.Text)-1;

                            }

                            if (i==pager.Controls.Count-3)

                            {

                                 down = int.Parse(l.Text)+1;

                            }

 

                            l.Text = "["+l.Text+"]";

                            l.ForeColor = System.Drawing.Color.Red;

                       }

                   }

 

                   Object oo = pager.Controls[0];

                   if (oo is LinkButton)

                   {

                       LinkButton h = (LinkButton) oo;

                       if (h.Text=="...")

                       {

                            h.ToolTip = "跳轉(zhuǎn)到第"+up.ToString()+"";

                       }

                   }

 

                   Object ooo = pager.Controls[pager.Controls.Count-1];

                   if (ooo is LinkButton)

                   {

                       LinkButton h = (LinkButton) ooo;

                       if (h.Text=="...")

                       {

                            h.ToolTip = "跳轉(zhuǎn)到第"+down.ToString()+"";

                       }

                   }

 

                   Label la = new Label();

                   la.Text = "<table width=100%><tr><td align=left>總記錄數(shù):<b>"+ViewState["Row"].ToString()+" </b>";

                   la.Text += "總頁數(shù):<b>"+this.DataGrid1.PageCount+" </b>";

                   la.Text += "當(dāng)前頁:<font color=red><b>"+(this.DataGrid1.CurrentPageIndex+1).ToString()+" </b></font></td><td align=right>";

                   pager.Controls.AddAt(0,la);

 

                   Label lb = new Label();

                   lb.Text = "</td></tr></table>";

                   pager.Controls.Add(lb);

              }

         }

     }

}



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1579739

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多