十天學(xué)會ASP.net之第九天
學(xué)習(xí)目的:學(xué)會用DATAGRID分頁顯示
雖然說DATAGRID的分頁顯示效率不怎么高,但是不管怎么說,它是最方便的分頁顯示,對于做一個(gè)留言簿還是相當(dāng)簡單實(shí)用的。對于啟用分頁功能還有一個(gè)重要要求,只能在DATADRID控件的數(shù)據(jù)源實(shí)現(xiàn)了ICOLLECTION接口的情況下才能啟用分頁功能,而DATAREADER沒有這個(gè)接口,所以必須使用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 " 調(diào)用第×頁的函數(shù)為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 " 調(diào)用第×頁的函數(shù)為dgrdMain_PageIndexChanged()
runat="server"
/>
</body>
</html>
這樣就是最簡單的分頁,我們可以用參數(shù)pagestyle-mode="nextprev/numericpages"來設(shè)定是顯示上一頁、下一頁方式還是直接選擇頁面的數(shù)字方式來實(shí)現(xiàn)頁面跳轉(zhuǎn)。如果是前者我們可以用pagestyle-nextpagetext來設(shè)定包含下一頁鏈接的文本,用pagestyle-prevpagetext來設(shè)定包含上一頁鏈接的文本;如果是后者我們可以用PagerStyle-PageButtonCount來設(shè)定在使用省略號之前顯示的頁面編號數(shù)量(默認(rèn)是10)
今天就說到這里,明天說一下SESSION和COOKIE。
關(guān)鍵詞:ASP.net
閱讀本文后您有什么感想? 已有 人給出評價(jià)!
- 0
- 0
- 0
- 1
- 0
- 0