親密接觸ASP.Net(13) 實現個人化分頁

來源:互聯網
上載者:User

個人化的分頁實現 我們前面講的分頁,只不過是通過修改DataGrid的屬性來實現分頁,這樣有這樣的好處,最大的就是簡單,呵呵,根本不用操心,分頁是如何產生的。

但是它同樣有缺點,不能按照我們想像的產生各種我們需要的樣式。

沒有辦法,想個人化功能,只有自已動手來做了,呵呵。

我們一步步的來,首先是匯入需要的命名空間。下面的例子,其實也是我從國外找來的,再加上點個人的東東,再漢化,呵呵。今天心情很好,我連標籤色彩都給大家顯示出來了。呵呵,更利於大家看程式。

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>

我們先看看,我們的web控制項是哪些,再看看代碼是怎麼寫的,這樣比較好:)

<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>

從上面的例子我們可以看出點擊LinkButton控制項onClick觸發的是PageButtonClick事件,DataGrid頁面OnPageIndexChanged改變觸發的是MyDataGrid_Page事件,我們以後就是就是要編寫這兩件事件的代碼

下面是先要查詢的資料庫的資訊,用一個函數表示,因為經常用到:),我開啟的表,呵呵,是我們工作室管理區的登陸記錄表(哈又賣了點我們工作室的秘密給大家)

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;
}

然後中是Page_Load函數,在這裡主要是判斷一下是否顯示DataGrid內建的那些分頁數字,使用的是PageStyle的Visible屬性:

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

BindGrid();
}

下面是處理點擊事件的PagerButtonClick,這是我們的核心部分,其實我們操作的也只是DataGrid的CurrentPageIndex屬性。如果CurrentPageIndex小於PageCount則有下一頁,如果CurrentPageIndex大於0則表示有前一頁。

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();
}

下面是MyDataGrid_Page,主要操作是調用BindGrid函數,以將資料交給DataGrid顯示:

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;
}

到此為止,我們的個人化頁面已經完成了(全部代碼和顯示看一下節),我們總的思想就是使用LinkButton控制項做為翻頁的標幟,通過判斷LinkButton的CommandArgument值,操作DataGrid的CurrentPageIndex屬性,以達到翻頁的效果。

如果大家在本節中看不懂結構,請參看下一節的全部代碼以及樣本。



相關文章

聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.