showProduct_dataList2 .aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="showProduct_dataList2 .aspx.cs" Inherits="showProduct_dataList" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="dl_showProduct" runat="server" CellPadding="4" DataKeyField="ProductID" ForeColor="#333333" RepeatColumns="4" RepeatDirection="Horizontal" OnItemCommand="dl_showProduct_ItemCommand" OnItemDataBound="dl_showProduct_ItemDataBound">
<AlternatingItemStyle BackColor="White" ForeColor="#284775" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<%--页脚分页开始--%>
<footertemplate>
共<asp:label id="totalPage" runat="server" text="totalPage"></asp:label>页
第<asp:label id="currentPage" runat="server" text="currentPage"></asp:label>页
<asp:linkbutton id="lb_firstPage" runat="server" CommandName="lb_firstPage">首页</asp:linkbutton>
<asp:linkbutton id="lb_prePage" runat="server" CommandName="lb_prePage">上一页</asp:linkbutton>
<asp:linkbutton id="lb_nextPage" runat="server" CommandName="lb_nextPage">下一页</asp:linkbutton>
<asp:linkbutton id="lb_lastPage" runat="server" CommandName="lb_lastPage">尾页</asp:linkbutton>
跳转到第<asp:TextBox ID="tb_search" runat="server"></asp:TextBox>页<asp:Button ID="btn_search" runat="server" Text="GO" CommandName="search" />
</footertemplate>
<%--页脚分页结束--%>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("phot", "{0}") %>' Height="100px" Width="100px" />
<br />
商品ID:
<asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' />
<br />
商品名称:
<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>' />
<br />
库存量:
<asp:Label ID="QuantityLabel" runat="server" Text='<%# Eval("Quantity") %>' />
<br />
单价:
<asp:Label ID="UnitLabel" runat="server" Text='<%# Eval("Unit") %>' />
<br /><br />
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='~/images/goumai.jpg' Width="100px" />
<br />
</ItemTemplate>
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:DataList>
</div>
</form>
</body>
</html>
showProduct_dataList2 .aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class showProduct_dataList : System.Web.UI.Page
{
protected static PagedDataSource ps = new PagedDataSource(); // 分页数据源对象
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBind(0);
}
}
private void DataBind(int CurrentPage) // 参数:页码
{
String strconn = System.Configuration.ConfigurationManager.ConnectionStrings["shopdataConnectionString"].ToString();
SqlConnection conn = new SqlConnection();
conn.ConnectionString = strconn;
conn.Open(); // 打开连接
string str = "select * from dbo.Product;";
SqlDataAdapter myadapt = new SqlDataAdapter(str, conn);
DataSet myds = new DataSet(); // 分页数据源
myadapt.Fill(myds, "mytab");
// 定义分页数据源的相关属性
ps.AllowPaging = true; // 允许分页
ps.PageSize = 12; // 每页显示条数
ps.DataSource = myds.Tables["mytab"].DefaultView; // 分页数据源的数据源
ps.CurrentPageIndex = CurrentPage; // 当前页码
dl_showProduct.DataSource = ps; // <asp: DataList ID="dl_showProduct"> </asp: DataList>
dl_showProduct.DataBind(); // 数据绑定
}
protected void dl_showProduct_ItemCommand(object source, DataListCommandEventArgs e)
{
switch (e.CommandName) {
case "lb_firstPage":
{
ps.CurrentPageIndex = 0;
DataBind(ps.CurrentPageIndex);
break;
}
case "lb_prePage":
{
if (ps.IsFirstPage)
{
ps.CurrentPageIndex = 0;
}
else
{
ps.CurrentPageIndex = ps.CurrentPageIndex - 1;
}
DataBind(ps.CurrentPageIndex);
break;
}
case "lb_nextPage":
{
if (ps.IsLastPage)
{
ps.CurrentPageIndex = ps.PageCount - 1;
}
else
{
ps.CurrentPageIndex = ps.CurrentPageIndex + 1;
}
//ps.CurrentPageIndex = ps.CurrentPageIndex + 1;
DataBind(ps.CurrentPageIndex);
break;
}
case "lb_lastPage":
{
ps.CurrentPageIndex = ps.PageCount - 1;
DataBind(ps.CurrentPageIndex);
break;
}
case "search":
{
TextBox tb_search = e.Item.FindControl("tb_search") as TextBox;
int index = Convert.ToInt32(tb_search.Text.ToString()); // 第 index 页
DataBind(index - 1);
break;
}
}
}
protected void dl_showProduct_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer) //获取FooterTemplate中的Label
{
Label total = e.Item.FindControl("totalPage") as Label;
total.Text = ps.PageCount.ToString(); // 共。页
Label current = e.Item.FindControl("currentPage") as Label;
current.Text = ps.CurrentPageIndex + 1 + ""; // 第。页
}
}
}