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