第一次发文章,请大家多多包含,后面会陆续发出其他分页方式。
DataList控件的灵活性简直无可挑剔了,但是没有把分页像Gridview那样封装进去,下面和大家一起分享一下用 PagedDataSource的分页....
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList.aspx.cs" Inherits="DataList" %>无标题页
后台代码:
using System;using System.Data;using System.Configuration;using System.Collections;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;public partial class DataList : System.Web.UI.Page{ SqlConnection sqlcon = new SqlConnection("server=.;Database=DBDemo;user id=sa;pwd=123"); public string strsql = "SELECT * FROM BookMarks"; protected void Page_Load(object sender, EventArgs e) { //设置数据源 PagedDataSource page = new PagedDataSource(); SqlDataAdapter adp = new SqlDataAdapter(strsql, sqlcon); sqlcon.Open(); DataSet dst = new DataSet(); adp.Fill(dst, "table"); DataTable tab = new DataTable(); tab=dst.Tables["table"]; page.DataSource = tab.DefaultView; //分页实现 page.PageSize = 3; page.AllowPaging = true; int CurPage; if (Request.QueryString["Page"] != null) { CurPage = Int32.Parse(Request.QueryString["Page"]); } else CurPage = 1; page.CurrentPageIndex = CurPage - 1; this.Label1.Text = CurPage.ToString(); this.Label2.Text = page.PageCount.ToString(); if (!page.IsFirstPage && CurPage > 1) { this.HyperLink2.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1); } if (!page.IsLastPage && CurPage < page.PageCount) { this.HyperLink3.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1); } this.HyperLink1.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1"; this.HyperLink4.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(page.PageCount.ToString()); DataList1.DataSource = page; DataList1.DataBind(); sqlcon.Close(); } }