程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> 親密接觸ASP.Net(14)

親密接觸ASP.Net(14)

編輯:ASP技巧
個性化分頁(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)
{
    //判斷是否隱藏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)
{
    //獲得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:
            //本頁值
            MyDataGrid.CurrentPageIndex = arg.ToInt32();
            break;
    }
    BindGrid();
}

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

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

void ShowStats()
{
    //顯示頁面信息
    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"
OnClick="PagerButtonClick"
/>
&nbsp;
<ASP:LinkButton id="btnPrev" runat="server"
Text="前頁"
CommandArgument="prev"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
OnClick="PagerButtonClick"
/>
&nbsp;
<ASP:LinkButton id="btnNext" runat="server"
Text="後頁"
CommandArgument="next"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
OnClick="PagerButtonClick"
/>
&nbsp;

<ASP:LinkButton id="btnLast" runat="server"
Text="末頁"
CommandArgument="last"
ForeColor="navy"
Font-Name="verdana" Font-size="8pt"
OnClick="PagerButtonClick"
/>


<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

看,我們前兩節看到的那些小數字也出來了:)
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved