第七天
学习目的学会用DATAGRID分页显示
虽然说DATAGRID的分页显示效率不怎么高但是不管怎么说它是最方便的分页显示对于做一个留言簿还是相当简单实用的对于启用分页功能还有一个重要要求只能在DATADRID控件的数据源实现了ICOLLECTION接口的情况下才能启用分页功能而DATAREADER没有这个接口所以必须使用DATATABLE来代替
<script runat=server language=c#>
void Page_Load()
{
string strConnection=Provider=MicrosoftJetOleDb;Data Source=;
strConnection+=ServerMapPath(guestbookmdb);
OleDbConnection objConnection=new OleDbConnection(strConnection);
OleDbDataAdapter objDataAdapter=new OleDbDataAdapter(select * from guestbookobjConnection);
DataSet objDataSet=new DataSet();
objDataAdapterFill(objDataSet);
dgrdMainDataSource=objDataSet;
dgrdMainDataBind();
}
void dgrdMain_PageIndexChanged(Object sender DataGridPageChangedEventArgs e)
{
dgrdMainCurrentPageIndex = eNewPageIndex;
DataBind();
}
</script>
<html>
<body>
<asp:DataGrid
id=dgrdMain
cellpadding=
showheader=true
borderwidth=
allowpaging=true 启用分页功能
pagesize= 每一页显示三条记录
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=MicrosoftJetOleDb;Data Source=+ServerMapPath(guestbookmdb))
objDataAdapter=new OleDbDataAdapter(select * from guestbookobjConnection)
objDataSet=new DataSet
objDataAdapterFill(objDataSet)
dgrdMainDataSource=objDataSet
dgrdMainDataBind()
end sub
sub dgrdMain_PageIndexChanged(sender as objecte as datagridpagechangedeventargs)
dgrdMainCurrentPageIndex = eNewPageIndex
DataBind
end sub
</script>
<html>
<body>
<asp:DataGrid
id=dgrdMain
cellpadding=
showheader=true
borderwidth=
allowpaging=true 启用分页功能
pagesize= 每一页显示三条记录
onpageindexchanged=dgrdMain_pageindexchanged 调用第×页的函数为dgrdMain_PageIndexChanged()
runat=server
/>
</body>
</html>
这样就是最简单的分页我们可以用参数pagestylemode=nextprev/numericpages来设定是显示上一页下一页方式还是直接选择页面的数字方式来实现页面跳转如果是前者我们可以用pagestylenextpagetext来设定包含下一页链接的文本用pagestyleprevpagetext来设定包含上一页链接的文本如果是后者我们可以用PagerStylePageButtonCount来设定在使用省略号之前显示的页面编号数量(默认是)
今天就说到这里明天说一下SESSION和COOKIE