手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >如何在DataGrid控件中实现自定义分页
如何在DataGrid控件中实现自定义分页
摘要:如何在DataGrid控件中实现自定义分页在一般情况下,DataGrid控件每次实现翻页操作时,都会将数据源中的数据重新调用一次,当数据中数...

如何在DataGrid控件中实现自定义分页

在一般情况下,DataGrid控件每次实现翻页操作时,都会将数据源中的数据重新调用一次,当数据中

数据很多时,这样做就会很浪费系统资源和降低程序的执行效率.这时候我们一般通过自定义分页来解

决这个问题.

DataGrid控件的AllowCustomPaging属性用来获取或设置DataGrid控件是否允许自定义分

页;VirtualItemCoun属性用来获取或设置在使用自定义分页时DataGrid中实际的项数.要实现自定义分

页,必须将AllowPaging与AllowCustomPaging属性都设置为"True".

在DataGrid中要实现自定义分页的关键是,使该控件仅仅调用当前显示所需要的数据源数据,在下

面的例子中通过CurrentPageIndex和PageSize属性的值,在数据绑定时只取当前页需要的数据.

(1)页面代码:

复制代码 代码如下:

<%@Pagelanguage="c#"Codebehind="Main.aspx.cs"AutoEventWireup="false"

Inherits="SissonDemo.Main"%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">

<HTML>

<HEAD>

<title>Main</title>

<metaname="GENERATOR"Content="MicrosoftVisualStudio.NET7.1">

<metaname="CODE_LANGUAGE"Content="C#">

<metaname="vs_defaultClientScript"content="JavaScript">

<metaname="vs_targetSchema"

content="http://schemas.microsoft.com/intellisense/ie5">

</HEAD>

<bodyMS_POSITIONING="GridLayout">

<formid="Form1"method="post"runat="server">

<FONTface="宋体">

<asp:DataGridid="DataGrid1"style="Z-INDEX:101;LEFT:8px;POSITION:

absolute;TOP:24px"runat="server"

Width="792px"Height="96px"AllowCustomPaging="True"

AllowPaging="True"PageSize="5">

<PagerStyleMode="NumericPages"></PagerStyle>

</asp:DataGrid></FONT>

</form>

</body>

</HTML>

(2)后台代码:

复制代码 代码如下:

usingSystem;

usingSystem.Collections;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Web;

usingSystem.Web.SessionState;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.HtmlControls;

usingSystem.Data.SqlClient;

namespaceSissonDemo

{

/**////<summary>

///Main的摘要说明。

///</summary>

publicclassMain:System.Web.UI.Page

{

protectedSystem.Web.UI.WebControls.DataGridDataGrid1;

intstartIndex=0;//用来保存当前页数据项的起始索引

privatevoidPage_Load(objectsender,System.EventArgse)

{

if(!IsPostBack)

{

Bind();//初始化时进行数据绑定

}

}

voidBind()//绑定数据方法

{//定义数据库连接对象

SqlConnectioncn=newSqlConnection("server=.;database=pubs;uid=sa;pwd=");

//创建数据适配对象

SqlDataAdapterda=newSqlDataAdapter("selecttitle_id,title,type,pub_id

,price,pubdatefromtitles",cn);

//创建DataSet对象

DataSetds=newDataSet();

try

{//从指定的索引开始取PageSize条记录.

da.Fill(ds,startIndex,DataGrid1.PageSize,"CurDataTable");

da.Fill(ds,"AllDataTable");//填充数据集合

//设置DataGrid控件实际要显示的项数

DataGrid1.VirtualItemCount=ds.Tables["AllDataTable"].Rows.Count;

//进行数据绑定

DataGrid1.DataSource=ds.Tables["CurDataTable"];

DataGrid1.DataBind();

}

catch

{

Page.RegisterClientScriptBlock("","<script>alert('数据显示错

误');</script>");

}

}

Web窗体设计器生成的代码#regionWeb窗体设计器生成的代码

overrideprotectedvoidOnInit(EventArgse)

{

//

//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。

//

InitializeComponent();

base.OnInit(e);

}

/**////<summary>

///设计器支持所需的方法-不要使用代码编辑器修改

///此方法的内容。

///</summary>

privatevoidInitializeComponent()

{

this.DataGrid1.PageIndexChanged+=new

System.Web.UI.WebControls.DataGridPageChangedEventHandler

(this.DataGrid1_PageIndexChanged);

this.Load+=newSystem.EventHandler(this.Page_Load);

}

#endregion

privatevoidDataGrid1_PageIndexChanged(objectsource,

System.Web.UI.WebControls.DataGridPageChangedEventArgse)

{

//设置DataGrid当前页的索引值为用户选择的页的索引

DataGrid1.CurrentPageIndex=e.NewPageIndex;

//取得当前页为止总共有多少条记录,以便在下一页就从该记录开始读取

startIndex=DataGrid1.PageSize*DataGrid1.CurrentPageIndex;

//取得绑定数据

Bind();

}

}

}

在这段程序中,首先在数据绑定时,设置DataGrid控件的VirtualItemCoun属性值为查询结果集中的

记录总数,然后取得当前页要显示的数据,初始化时当前页显示的数据为从取到的数据的零位置开始,到

DataGrid控件的PageSize属性的设定值为止的数据记录数.在分页操作中重新定义了取得下一页数据时

的数据项的开始索引值,然后调用数据绑定方法把取到的新数据和DataGrid控件重新绑定.

【如何在DataGrid控件中实现自定义分页】相关文章:

asp.net中调用winrar实现压缩解压缩的代码

DataGrid 动态添加模板列 实现代码

ASP.NET2.0服务器控件之自定义状态管理

Net中实现无限分类的2个例子

获取转向地址的URL的源文件(可自定义REFER)

asp.net datalist绑定数据后可以上移下移实现示例

在asp.net中实现datagrid checkbox 全选的方法

asp.net SAF 中缓存服务的实现第1/5页

asp.net 文件下载实现代码

在.NET2.0中使用自定义事务操作

精品推荐
分类导航