学习目的:学会用DATAGRID分页显示
虽然说DATAGRID的分页显示效率不怎么高,但是不管怎么说,它是最方便的分页显示,对于做一个留言簿还是相当简单实用的。对于启用分页功能还有一个重要要求,只能在DATADRID控件的数据源实现了ICOLLECTION接口的情况下才能启用分页功能,而DATAREADER没有这个接口,所以必须使用DATATABLE来代替。
<script runat="server" language="c#">
void Page_Load()
{
string strConnection="Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
strConnection+=Server.MapPath("guestbook.mdb");
OleDbConnection objConnection=new OleDbConnection(strConnection);
OleDbDataAdapter objDataAdapter=new OleDbDataAdapter("select * from guestbook",objConnection);
DataSet objDataSet=new DataSet();
objDataAdapter.Fill(objDataSet);
dgrdMain.DataSource=objDataSet;
dgrdMain.DataBind();
}
void dgrdMain_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
{
dgrdMain.CurrentPageIndex = e.NewPageIndex;
DataBind();
}
</script>
<html>
<body>
<asp:DataGrid
id="dgrdMain"
cellpadding="1"
showheader="true"
borderwidth="0"
allowpaging="true" 启用分页功能
pagesize="3" 每一页显示三条记录
onpageindexchanged="dgrdMain_pageindexchanged " 调用第×页的函数为dgrdMain_PageIndexChanged()
runat="server"
/>
</body>
</html>
--------------------------------------------------------------------------------
<script runat="server" language="vb">
subPage_Load
dim objConnection as OleDbConnection
dim objDataAdapter as OleDbDataAdapter
dim objDataSet as DataSet
objConnection=new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath("guestbook.mdb"))
objDataAdapter=new OleDbDataAdapter("select * from guestbook",objConnection)
objDataSet=new DataSet
objDataAdapter.Fill(objDataSet)
dgrdMain.DataSource=objDataSet
dgrdMain.DataBind()
end sub
sub dgrdMain_PageIndexChanged(sender as object,e as datagridpagechangedeventargs)
dgrdMain.CurrentPageIndex = e.NewPageIndex
DataBind
end sub
</script>
<html>
<body>
<asp:DataGrid
id="dgrdMain"
cellpadding="1"
showheader="true"
borderwidth="0"
allowpaging="true" 启用分页功能
pagesize="3" 每一页显示三条记录
onpageindexchanged="dgrdMain_pageindexchanged " 调用第×页的函数为dgrdMain_PageIndexChanged()
runat="server"
/>
</body>
</html>
这样就是最简单的分页,我们可以用参数pagestyle-mode="nextprev/numericpages"来设定是显示上一页、下一页方式还是直接选择页面的数字方式来实现页面跳转。如果是前者我们可以用pagestyle-nextpagetext来设定包含下一页链接的文本,用pagestyle-prevpagetext来设定包含上一页链接的文本;如果是后者我们可以用PagerStyle-PageButtonCount来设定在使用省略号之前显示的页面编号数量(默认是10)
今天就说到这里,明天说一下SESSION和COOKIE。