AspNetPager分頁控制項使用方法(不使用DBAccess.cs檔案)(二)

來源:互聯網
上載者:User
 

AspNetPager分頁控制項使用方法(不使用DBAccess.cs檔案)一、前台顯示介面代碼Default.aspx(注意,代碼運行環境是VS.2005)<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default4" %><%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %><html xmlns="http://www.w3.org/1999/xhtml" ><head id="Head1" runat="server">    <title>用AspNetPager.dll控制項的分頁方法操作方法</title></head><body>    <form id="form1" runat="server">    <div>    <table border=1>       <asp:Repeater ID="Repeater1" runat="server">        <ItemTemplate>        <tr>        <td><%#DataBinder.Eval(Container.DataItem, "city_id")%></td>        <td><%#DataBinder.Eval(Container.DataItem, "city_name")%></td>        </tr>        </ItemTemplate>        </asp:Repeater>    </table>  <webdiyer:AspNetPager ID="AspNetPager1" runat="server" Width="100%" NumericButtonCount="6" UrlPaging="true" NumericButtonTextFormatString="[{0}]" CustomInfoHTML="第 <font color='red'><b>%CurrentPageIndex%</b></font> 頁 共 %PageCount% 頁 顯示 %StartRecordIndex%-%EndRecordIndex% 條" ShowCustomInfoSection="left"FirstPageText="首頁" LastPageText="末頁" NextPageText="下頁" PrevPageText="上頁" Font-Names="Arial" BackColor="#F8B500" AlwaysShow="true" ShowInputBox="Always" SubmitButtonText="跳轉" SubmitButtonStyle="botton" OnPageChanged="AspNetPager1_PageChanged" >              </webdiyer:AspNetPager>// NumericButtonCount="6"                 顯示頁碼的個數 // NumericButtonTextFormatString="[{0}]" 頁碼顯示的格式// ShowCustomInfoSection="left"           顯示左邊的(第幾頁、共幾頁、顯示幾條)// TextBeforeInputBox="轉到第"            InputBox前面顯示的字// TextAfterInputBox="頁"                 InputBox後面顯示的字// PagingButtonSpacing="10px"             字與字間的距離    </div>    </form></body></html>二、Default.aspx.cs頁面的代碼using System.Data.SqlClient;public partial class Default : System.Web.UI.Page{    //SqlConnection con = connsql.ZYJCreateConnection();    protected void Page_Load(object sender, EventArgs e)    {        if (!Page.IsPostBack)        {            BindGrid();        }    }     private SqlConnection con;    //private string DBName = "tongjinet";     //建立連線物件並開啟    public void Open()    {        if (con == null)          //con = new SqlConnection("server=(local);uid=sa;pwd=sql;database=" + DBName);            con = connsql.ZYJCreateConnection();        if (con.State == ConnectionState.Closed)            con.Open();    }     //建立一個命令對象並返回該對象    public SqlCommand CreateCommand(string sqlStr)    {        Open();        SqlCommand cmd = new SqlCommand();        cmd.CommandType = CommandType.Text;        cmd.CommandText = sqlStr;        cmd.Connection = con;        return cmd;    }     //產生一個對象並返回該結果集第一行第一列    public object GetScalar(string sqlStr)    {        SqlCommand cmd = CreateCommand(sqlStr);        object obj = cmd.ExecuteScalar();        //CommadnBehavior.CloseConnection是將於DataReader的資料庫連結關聯起來         //當關閉DataReader對象時候也自動關閉連結        return obj;    }     //執行資料庫查詢並返回一個資料集 [當前頁碼,每頁記錄條數]    public DataSet GetCurrentPage(int pageIndex, int pageSize)    {        //設定匯入的起始地址        int firstPage = pageIndex * pageSize;        string sqlStr = "select * from city order by city_id desc";        SqlCommand cmd = CreateCommand(sqlStr);        DataSet dataset = new DataSet();        SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);        dataAdapter.Fill(dataset, firstPage, pageSize, "city");        cmd.Dispose();        Close();        dataAdapter.Dispose();        return dataset;    }    //獲得查詢資料的總條數    public object GetAllCount()    {        string sqlStr = "select count(*) from city";        object obj = GetScalar(sqlStr);        return obj;    }    //關閉資料庫    public void Close()    {        if (con != null)        {            con.Close();        }    }    //釋放資源    public void Dispose()    {        if (con != null)        {            con.Dispose();            con = null;        }    }     protected void AspNetPager1_PageChanged(object sender, EventArgs e)    {        BindGrid();    }     public void BindGrid()    {        this.AspNetPager1.RecordCount = Int32.Parse(GetAllCount().ToString());        int pageIndex = this.AspNetPager1.CurrentPageIndex - 1;        int pageSize = this.AspNetPager1.PageSize = 5;        Repeater1.DataSource = GetCurrentPage(pageIndex, pageSize);        Repeater1.DataBind();    }} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.