页面源码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="city" runat="server">
<ItemTemplate>
id:
<asp:Label ID="idLabel" runat="server" Text='<%# Ev
pro_id:
<asp:Label ID="pro_idLabel" runat="server" Text='<%# Ev
city:
<asp:Label ID="cityLabel" runat="server" Text='<%# Ev
<br />
</ItemTemplate>
</asp:DataList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connsky %>"
SelectCommand="SELECT city.* FROM city"></asp:SqlDataSource>
<asp:LinkButton ID="btnFirst" runat="server" On
<asp:LinkButton ID="btnPrevious" runat="server" On
(<asp:Label ID="lblPageIndex" runat="server" Text="Label"></asp:Label>/<asp:Label
ID="lblPageCount" runat="server" Text="Label"></asp:Label>) <asp:LinkButton
ID="btnNext" runat="server" On
<asp:LinkButton ID="btnLast" runat="server" On
</div>
</form>
</body>
</html>
----------------------------------------------------------------------------------------------------------------------------
.cs代码
using System;
using System.Da
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.Da
public partial class _Default : System.Web.UI.Page
{
//控件开发人员需对自定义数据绑定控件提供分页支持时,可使用的类PagedDatasource
public static PagedDataSource pds = new PagedDataSource();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
pds.AllowPaging = true;//允许分页
pds.CurrentPageIndex = 0;//当前页的索引设置为0,即第一页的索引为0
pds.PageSize = 4;//单页显示的项数
}
RefreshStatus();
}
//流程:1.现在页面加载的时候,把PagedDataSouce的参数设置好(如单页显示多少条记录)
//2.pds.DataSource=AdminManager.GetAllAdmins();这样就变成有分页的数据了,然后再和DataList绑定就好了
private void RefreshStatus()
{
string strCon ="Da
SqlConnection con = new SqlConnection(strCon);
SqlDataAdapter da = new SqlDataAdapter("select * from city", con);
DataSet ds = new DataSet();
con.Open();
da.Fill(ds, "city");
pds.DataSource = ds.Tables["city"].DefaultView;
city.DataSource = pds;
city.DataBind();
btnFirst.Enabled = !pds.IsFirstPage;//判断当前页是不是第一页,如果是,那么"第一页"的按钮就应该不可用
btnPrevious.Enabled = !pds.IsFirstPage; ;//判断当前页是不是第一页,如果是,那么"上一页"的按钮就应该不可用
btnNext.Enabled = !pds.IsLastPage;
btnLast.Enabled = !pds.IsLastPage;
lblPageIndex.Text = Convert.ToString(pds.CurrentPageIndex + 1);//如果当前页是第一页,pds.CurrentPageIndex的值会为0,所以要+1
lblPageCount.Text = pds.PageCount.ToString();
}
protected void btnFirst_Click(object sender, EventArgs e)
{
pds.CurrentPageIndex = 0;//如果是第一页,就把pds.CurrentPageIndex参数设置为0,然后在绑定
RefreshStatus();
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
pds.CurrentPageIndex -= 1;
RefreshStatus();
}
protected void btnNext_Click(object sender, EventArgs e)
{
pds.CurrentPageIndex += 1;
RefreshStatus();
}
protected void btnLast_Click(object sender, EventArgs e)
{
pds.CurrentPageIndex = pds.PageCount - 1;
RefreshStatus();
联系客服