/*
实现DataGrid控件分页显示的示例
*/
前台处理程序
//DataGridPageDemo.aspx
<%@ Page language="c#" Codebehind="DataGridPageDemo.aspx.cs"
AutoEventWireup="false" Inherits="DataGridPageDemo.DataGridPageDemo"
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebForm1</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="dgDemo" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server" Width="264px" Height="240px" AllowPaging="True"> <PagerStyle Mode="NumericPages"></PagerStyle> </asp:DataGrid></FONT> </form> </body> </HTML>
//下面的是后台处理程序
DataGridPageDemo.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 DataGridPageDemo { /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class DataGridPageDemo : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid dgDemo; private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 if(!IsPostBack) { DataFiller(); } }
#region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.dgDemo.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgDemo_PageIndexChanged); this.Load += new System.EventHandler(this.Page_Load);
} #endregion
private void DataFiller() { string strConn="server=JOSEN;database=NorthWind;integrated security=true"; string strSql="select * from orders"; SqlConnection objCon=new SqlConnection(strConn); SqlDataAdapter objAdapter=new SqlDataAdapter(strSql,objCon); DataSet ds=new DataSet(); objAdapter.Fill(ds,"dtorder");
dgDemo.DataSource=ds.Tables["dtorder"]; dgDemo.DataBind();
}
private void dgDemo_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { dgDemo.CurrentPageIndex=e.NewPageIndex; DataFiller(); } } }
|