1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PagedDataSourceone.aspx.cs" Inherits="PagedDataSourceone" %> 2 <!DOCTYPE html > 3 <html> 4 <head runat="server"> 5 <title>Datalist分页测试</title> 6 </head> 7 <body> 8 <form id="form1" runat="server"> 9 <asp:DataList ID="DataList1" runat="server" RepeatColumns="1" HorizontalAlign="Justify" RepeatDirection="Horizontal" Width="100%" EnableTheming="True"> 10 <ItemTemplate> 11 <%# Eval("MenuName")%> 12 </ItemTemplate> 13 </asp:DataList> 14 当前页:<asp:Label ID="lblCurrent" runat="server" Text="1"></asp:Label> 15 总页数:<asp:Label ID="lblTotal" runat="server" Text="Label"></asp:Label> 16 <asp:LinkButton ID="lbtnFirst" runat="server" OnClick="lbtnFirst_Click">第一页</asp:LinkButton> 17 <asp:LinkButton ID="lbntUp" runat="server" OnClick="lbntUp_Click">上一页</asp:LinkButton> 18 <asp:LinkButton ID="lbtnDown" runat="server" OnClick="lbtnDown_Click">下一页</asp:LinkButton> 19 <asp:LinkButton ID="lbtnLast" runat="server" OnClick="lbtnLast_Click">最后一页</asp:LinkButton> </form> 20 </body> 21 </html>
上面代码是前台部分.
1 using System; 2 using System.Collections.Generic; 3 using System.Web; 4 using System.Web.UI; 5 using System.Web.UI.WebControls; 6 using JajaReport.DBUtility;//必须引用 7 using JajaWeiXin.PublicClass;//必须引用 8 using System.Data.SqlClient; 9 using System.Data; 10 11 12 public partial class PagedDataSourceone : System.Web.UI.Page 13 { 14 protected void Page_Load(object sender, EventArgs e) 15 { 16 if (!IsPostBack) 17 { 18 DataListBind(); 19 } 20 } 21 /// <summary> 22 /// DataList1的列表 23 /// </summary> 24 protected void DataListBind() 25 { 26 //string MenuSQL = "Select MenuID,MenuName,ClassID,MenuUnit,MenuPrice,OriPrice,LimitNum,StockNum,IsRecom,IsHot,HotIndex,MenuIndex,MakeRemark,MenuDesc,UpdateTime from wx_bu_Menu where OriPrice>=0"; 27 //重构之后的代码调用 28 //PublicDataListBase.ShowDataList(DataList1, MenuSQL); 29 30 int current_page = Convert.ToInt32(lblCurrent.Text); 31 SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]); 32 SqlDataAdapter oda = new SqlDataAdapter("Select MenuID,MenuName,ClassID,MenuUnit,MenuPrice,OriPrice,LimitNum,StockNum,IsRecom,IsHot,HotIndex,MenuIndex,MakeRemark,MenuDesc,UpdateTime from wx_bu_Menu where OriPrice>=0", con); 33 DataSet ds = new DataSet(); 34 oda.Fill(ds); 35 36 PagedDataSource ps = new PagedDataSource(); 37 ps.DataSource = ds.Tables[0].DefaultView; 38 ps.AllowPaging = true; 39 ps.PageSize = 4; 40 lblTotal.Text = ps.PageCount.ToString(); 41 ps.CurrentPageIndex = current_page - 1; 42 lbtnFirst.Enabled = true; 43 lbntUp.Enabled = true; 44 lbtnDown.Enabled = true; 45 lbtnLast.Enabled = true; 46 if (current_page == 1) 47 { 48 lbtnFirst.Enabled = false; 49 lbntUp.Enabled = false; 50 } 51 if (current_page == Convert.ToInt32(lblTotal.Text)) 52 { 53 lbtnLast.Enabled = false; 54 lbtnDown.Enabled = false; 55 } 56 DataList1.DataSource = ps; 57 DataList1.DataBind(); 58 } 59 60 protected void lbtnFirst_Click(object sender, EventArgs e) 61 { 62 lblCurrent.Text = "1"; 63 DataListBind(); 64 } 65 protected void lbtnDown_Click(object sender, EventArgs e) 66 { 67 lblCurrent.Text = (Convert.ToInt32(lblCurrent.Text) + 1).ToString(); 68 DataListBind(); 69 } 70 protected void lbntUp_Click(object sender, EventArgs e) 71 { 72 lblCurrent.Text = (Convert.ToInt32(lblCurrent.Text) - 1).ToString(); 73 DataListBind(); 74 } 75 protected void lbtnLast_Click(object sender, EventArgs e) 76 { 77 lblCurrent.Text = lblTotal.Text; 78 DataListBind(); 79 } 80 }
以上代码时候台部分.
Datalist实现分页
时间: 2024-10-05 08:18:54