ASP.NET技巧:DataGrid傳統分頁方式_.Net教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:.NET環境下幾種不同的郵件發送解決方案1、使用outLook提供的發送 如:我見過的用友軟件U8生產制造(演示版),其中的郵件功能就是通過調用outLook的ActiveX 組件 優點:開發簡單 缺點:依賴outlook組件,SMTP 郵件服務 郵件發
此分頁方式與傳統ASP分頁方式相仿。
DataGridPage.aspx
以下為引用的內容: <%@ Page language="c#" Codebehind="DataGridPage.aspx.cs" AutoEventWireup="false" Inherits="netCRM.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"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:datagrid id="DataGrid1" runat="server" AlternatingItemStyle-BackColor="#eeeeee" HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="Verdana" CellPadding="3" BorderWidth="1px" BorderColor="Black" PagerStyle-HorizontalAlign="Right" PagerStyle-Mode="NumericPages" PageSize="5" Font-Names="Verdana" Width="100%"> <AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle> <HeaderStyle BackColor="#AAAADD"></HeaderStyle> <PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle> </asp:datagrid> </form> <TABLE cellSpacing="0" cellPadding="1" width="100%" bgColor="#aaaadd" border="0"> <TBODY> <TR> <TD> <TABLE cellSpacing="0" cellPadding="4" width="100%" bgColor="#fef8e2" border="0"> <TBODY> <TR> <TD class="M" noWrap align="center"><asp:Literal id="Literal1" runat="server"></asp:Literal></TD> </TR> <TR> <TD class="C" noWrap align="center"><asp:Literal id="Literal2" runat="server"></asp:Literal></TD> </TR> </TBODY> </TABLE> </TD> </TR> </TBODY> </TABLE> </body> </HTML> |
DataGridPage.aspx.cs
以下為引用的內容: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace netCRM { /// <summary> /// DataGridPage 的摘要說明。 /// </summary> public class DataGridPage : System.Web.UI.Page { protected System.Web.UI.WebControls.Literal Literal1; protected System.Web.UI.WebControls.Literal Literal2; protected System.Web.UI.WebControls.DataGrid DataGrid1; private void Page_Load(object sender, System.EventArgs e) { // 在此處放置用戶代碼以初始化頁面 if(!IsPostBack) { BindGrid(); } } private void BindGrid() { string connstring = "Server=.;Database=NorthWind;User Id=sa;Password=;"; string sql="Select * from Orders"; SqlConnection conn = new SqlConnection(connstring); conn.Open(); DataSet ds = new DataSet(); SqlDataAdapter sqlAdapter = new SqlDataAdapter(sql,conn); sqlAdapter.Fill(ds,"users"); DataView dataview = new DataView(); dataview = ds.Tables[0].DefaultView; DataGrid1.DataSource = ds.Tables[0].DefaultView; DataGrid1.DataBind(); string cPage; int pageSize = 10; int currentPage; int pageCount; int numResults = 0; if (Request.QueryString["page"]==null) { cPage="1"; } else { cPage=Request.QueryString["page"].ToString(); } try { currentPage = Int32.Parse(cPage); } catch { currentPage = 1; } numResults = 0; int start = (int)((currentPage - 1) * pageSize); int to = (int)(currentPage * pageSize); if (start <= 0) start = 0; numResults = dataview.Count; int a1=0; pageCount = Math.DivRem(numResults,pageSize,out a1); if (a1>0) { pageCount ; } if(currentPage>pageCount || currentPage<=0) { currentPage = 1; } if(currentPage==pageCount) { to = dataview.Count; } // Create one DataTable with one column. DataTable myTable = new DataTable("myTable"); myTable = dataview.Table.Clone(); //DataColumn colItem1 = new DataColumn("name",Type.GetType("System.String")); //DataColumn colItem2 = new DataColumn("types",Type.GetType("System.String")); //DataColumn colItem3 = new DataColumn("vendor",Type.GetType("System.String")); //myTable.Columns.Add(colItem1); //myTable.Columns.Add(colItem2); //myTable.Columns.Add(colItem3); //add row DataRow NewRow; for(int i=start;i<numResults;i ) { if(i<to) { NewRow = myTable.NewRow(); for(int k=0;k<dataview.Table.Columns.Count;k ) { NewRow[k] = dataview.Table.Rows[i][k]; } myTable.Rows.Add(NewRow); } } myTable.AcceptChanges(); DataView resultDataview = new DataView(myTable); DataGrid1.DataSource = resultDataview; DataGrid1.DataBind(); /// <summary> /// 生成頁導航條。 /// </summary> string strNav = ""; int endpage; if (currentPage>1) { strNav = "<a href='?page=" (currentPage-1).ToString() "'>上一頁</a> "; } if (currentPage>11) { strNav = "<a href='?page=1'>1</a> ..."; } if(pageCount>currentPage 10) { endpage = currentPage 10; } else { endpage = pageCount; } for (int i=currentPage-10;i<endpage 1;i ) { if(i>=1) { if (i==currentPage) { strNav ="<font color=#990000><strong>" i.ToString() "</strong></font> "; } else { strNav = "<a href='?page=" i.ToString() "'>" i.ToString() "</a> "; } } } if((currentPage 10)<pageCount) { strNav = "... <a href='?page=" pageCount.ToString() "'>" pageCount.ToString() "</a>"; } if(currentPage<pageCount) { strNav = " <a href='?page=" (currentPage 1).ToString() "'>下一頁</a> "; } Literal1.Text = strNav; Literal2.Text = "共 " numResults.ToString() " 條供應信息,當前顯示第 " (start 1).ToString() " - " to.ToString() " 條,共 " pageCount.ToString() " 頁"; } #region Web 窗體設計器生成的代碼 override protected void OnInit(EventArgs e) { // // CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 設計器支持所需的方法 - 不要使用代碼編輯器修改 /// 此方法的內容。 /// </summary> private void InitializeComponent() { this.Load = new System.EventHandler(this.Page_Load); } #endregion } } |
分享:VB.NET實現窗體圖標最小化到狀態欄本文所使用的編程環境是Microsoft Visual Studio 2005,首先打開 Visual Studio。在文件 (File) 菜單上,單擊新建項目 (New Project)。 在新建項目 (New Project) 對話框的模板 (Templates) 窗
相關.Net教程:
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發送Email實例(可帶附件)
- js實現廣告漂浮效果的小例子
- asp.net Repeater 數據綁定的具體實現
- Asp.Net 無刷新文件上傳并顯示進度條的實現方法及思路
- Asp.net獲取客戶端IP常見代碼存在的偽造IP問題探討
- VS2010 水晶報表的使用方法
- ASP.NET中操作SQL數據庫(連接字符串的配置及獲取)
- asp.net頁面傳值測試實例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲過程實現分頁示例代碼
- 相關鏈接:
- 教程說明:
.Net教程-ASP.NET技巧:DataGrid傳統分頁方式。