SqlConnection (.NET C#) 連接及分頁(yè)
.net的訪(fǎng)問(wèn)數(shù)據(jù)機(jī)制決定了訪(fǎng)問(wèn)大量數(shù)據(jù)時(shí)會(huì)致使客戶(hù)端機(jī)器消耗大量資源,因此有必要對(duì)數(shù)據(jù)進(jìn)行分頁(yè)顯示,開(kāi)發(fā)工具vs.net+sqlserver,語(yǔ)言c#
1.加入引用
將AspNetPager控件引入到項(xiàng)目中,即在aspx頁(yè)面里添加引用,把AspNetPager的dll文件加到Bin文件夾目錄下
using System.Data.SqlClient;
using Wuqi.Webdiyer;
2.前臺(tái)顯示頁(yè)面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>無(wú)標(biāo)題頁(yè)</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><%#"客戶(hù)ID: "+Eval("customerid") %> <%#"公司名稱(chēng): "+Eval("companyname") %> <%#"聯(lián)系地址: "+Eval("address") %></li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
<br />
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" FirstPageText="" LastPageText=""
NextPageText="下一頁(yè)" OnPageChanging="AspNetPager1_PageChanging" CssClass="pages" CurrentPageButtonClass="cpb" PrevPageText="上一頁(yè)">
</webdiyer:AspNetPager>
</div>
</form>
</body>
</html>
3.后臺(tái)代碼部分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ù)源允許分頁(yè)
pdsList.PageSize = this.AspNetPager1.PageSize;//取控件的分頁(yè)大小
pdsList.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1;//顯示當(dāng)前頁(yè)
//設(shè)置控件
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;}
|