SqlConnection (.NET C#) 連接及分頁
.net的訪問數(shù)據(jù)機制決定了訪問大量數(shù)據(jù)時會致使客戶端機器消耗大量資源,因此有必要對數(shù)據(jù)進行分頁顯示,開發(fā)工具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./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./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") %> <%#"公司名稱: "+Eval("companyname") %> <%#"聯(lián)系地址: "+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;//數(shù)據(jù)源允許分頁
pdsList.PageSize = this.AspNetPager1.PageSize;//取控件的分頁大小
pdsList.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1;//顯示當前頁
//設置控件
this.AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;//記錄總數(shù)
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;}
|