(轉) .NET實現Repeater控制項+AspNetPager控制項分頁

來源:互聯網
上載者:User

標籤:style   http   color   os   io   for   ar   檔案   資料   

SqlConnection (.NET C#) 串連及分頁 
.net的訪問資料機制決定了訪問大量資料時會致使用戶端機器消耗大量資源,因此有必要對資料進行分頁顯示,開發工具vs.net+sqlserver,語言c#

1.加入引用

將AspNetPager控制項引入到項目中,即在aspx頁面裡添加引用,把AspNetPager的dll檔案加到Bin檔案夾目錄下
using System.Data.SqlClient;
using Wuqi.Webdiyer;

2.前台顯示頁面aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>無標題頁</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate><ul></HeaderTemplate>
            <ItemTemplate>
                <li><%#"客戶ID: "+Eval("customerid") %>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%#"公司名稱: "+Eval("companyname") %>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%#"聯絡地址: "+Eval("address") %></li>
            </ItemTemplate>
        <FooterTemplate></ul></FooterTemplate>
        </asp:Repeater>
        <br />
        <webdiyer:AspNetPager ID="AspNetPager1" runat="server" FirstPageText="" LastPageText=""
            NextPageText="下一頁" OnPageChanging="AspNetPager1_PageChanging" CssClass="pages" CurrentPageButtonClass="cpb" PrevPageText="上一頁">
        </webdiyer:AspNetPager>
    </div>
    </form>
</body>
</html>

3.後台代碼部分aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Wuqi.Webdiyer;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindsql();
        }
    }
    private void bindsql()
    {
        SqlConnection con = new SqlConnection("Data Source=20090731-5465;Initial Catalog=Northwind;Integrated Security=True");
        con.Open();
        SqlDataAdapter sqlda = new SqlDataAdapter("select * from customers", con);
        DataSet ds = new DataSet();
        sqlda.Fill(ds);
        PagedDataSource pdsList = new PagedDataSource();
        pdsList.DataSource = ds.Tables[0].DefaultView;
        pdsList.AllowPaging = true;//資料來源允許分頁
        pdsList.PageSize = this.AspNetPager1.PageSize;//取控制項的分頁大小
        pdsList.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1;//顯示當前頁
        //設定控制項
        this.AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;//記錄總數
        this.AspNetPager1.PageSize = 10;
        this.Repeater1.DataSource = pdsList;
        this.Repeater1.DataBind();

    }
    protected void AspNetPager1_PageChanging(object src, PageChangingEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        bindsql();
    }
}

4.CSS檔案樣式表

body {
}
.pages { color: #999; }
.pages a, .pages .cpb { text-decoration:none;float: left; padding: 0 5px; border: 1px solid #ddd;background: #ffff;margin:0 2px; font-size:11px; color:#000;}
.pages a:hover { background-color: #E61636; color:#fff;border:1px solid #E61636; text-decoration:none;}
.pages .cpb { font-weight: bold; color: #fff; background: #E61636; border:1px solid #E61636;}

(轉) .NET實現Repeater控制項+AspNetPager控制項分頁

聯繫我們

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