親密接觸ASP.Net(14) 個人化分頁(2)

來源:互聯網
上載者:User

個人化分頁(2) 上一節大家沒有看到總的結構,這一節,我把代碼全部貼出來,大家好好看看。

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
<html>
<script language="C#" runat="server">

void Page_Load(Object sender, EventArgs e)
{
file://判斷是否隱藏PagerStyle-Mode
if (chk1.Checked)
{
MyDataGrid.PagerStyle.Visible=true;
}
else
{
MyDataGrid.PagerStyle.Visible=false;
}

BindGrid();
}

ICollection CreateDataSource()
{
/*
讀取資料庫的資訊,獲得DataView
*/
SQLConnection MyConnection = new SQLConnection("server=localhost;uid=sa;pwd=123456;database=aspcn");
SQLDataSetCommand MyDataSetCommand = new SQLDataSetCommand("select * from admin_enter order by Enter_Time desc",MyConnection);
DataSet ds= new DataSet();
MyDataSetCommand.FillDataSet(ds,"admin_enter");
return ds.Tables["admin_enter"].DefaultView;
}

void PagerButtonClick(Object sender, EventArgs e)
{
file://獲得LinkButton的參數值
String arg = ((LinkButton)sender).CommandArgument;

switch(arg)
{
case ("next"):
if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
MyDataGrid.CurrentPageIndex ++;
break;
case ("prev"):
if (MyDataGrid.CurrentPageIndex > 0)
MyDataGrid.CurrentPageIndex --;
break;
case ("last"):
MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
break;
default:
file://本頁值
MyDataGrid.CurrentPageIndex = arg.ToInt32();
break;
}
BindGrid();
}

void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e)
{
file://處理按下數位方法
BindGrid();
}

void BindGrid()
{
file://將DataView綁定到DataGrid上去
MyDataGrid.DataSource = CreateDataSource();
MyDataGrid.DataBind();
ShowStats();
}

void ShowStats()
{
file://顯示頁面資訊
lblCurrentIndex.Text = "當前頁數為: " + ((int)MyDataGrid.CurrentPageIndex+1);
lblPageCount.Text = "總頁數是: " + MyDataGrid.PageCount;
}

</script>

<body>

<h3><font face="Verdana">個人化的分頁執行個體^&^</font></h3>

<form runat=server>

<ASP:DataGrid id="MyDataGrid" runat="server"
AllowPaging="True"
PageSize="10"
PagerStyle-Mode="NumericPages"
PagerStyle-HorizontalAlign="Right"
OnPageIndexChanged="MyDataGrid_Page"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AlternatingItemStyle-BackColor="#eeeeee"
AutoGenerateColumns="false"

>
<property name="Columns">
<asp:BoundColumn HeaderText="工作室成員" DataField="Enter_ID" />
<asp:BoundColumn HeaderText="登陸時間" DataField="Enter_Time" />
</property>
</ASP:DataGrid>

<p>
<asp:LinkButton id="btnFirst" runat="server"
Text="首頁"
CommandArgument="0"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"

/>
 
<asp:LinkButton id="btnPrev" runat="server"
Text="前頁"
CommandArgument="prev"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"

/>
 
<asp:LinkButton id="btnNext" runat="server"
Text="後頁"
CommandArgument="next"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"

/>
 

<asp:LinkButton id="btnLast" runat="server"
Text="末頁"
CommandArgument="last"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"

/>


<p>
<asp:Checkbox id="chk1" runat="server"
Text="顯示內建的頁數"
Font-Name="Verdana"
Font-Size="8pt"
AutoPostBack="true"
/>

<p>
<table bgcolor="#eeeeee" cellpadding="6"><tr><td nowrap><font face="Verdana" size="-2">

<asp:Label id="lblCurrentIndex" runat="server" /><br>
<asp:Label id="lblPageCount" runat="server" /><br>

</font></td></tr></table>
</form>

</body>

</html>

上面是全部的原始碼,執行後的顯示是:

圖14-1

按下隨便一個鍵,比如"末頁"

圖14-2

看,我們的程式就直接到了最後一頁,即第5頁,我們在下面的"當前頁數"可以看到:)

我們再來點擊"顯示內建的頁數"這個複選框,看看:

圖14-3

看,我們前兩節看到的那些小數字也出來了:)



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。