前台页面代码
4个按钮控件 首页 上一页 下一页 尾页
View Code
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
PagerDemp01.aspx.cs
"
Inherits
=
"
PagerDemo_PagerDemp01
"
Theme
=
""
%>
<! 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 > PagerDemo 01 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div style ="width: 100%; height: 500px; text-align:center; margin: 0 auto;" >
< asp:Repeater ID ="Repeater1" runat ="server" >
< ItemTemplate >
< table width ="500" style ="margin: 0 auto" >
< tbody >
< tr >
< td width ="50" > <% # DataBinder.Eval(Container.DataItem, " Id " ) %> </ td >
< td > <% # DataBinder.Eval(Container.DataItem, " ProductName " ) %> </ td >
< td width ="50" > <% # DataBinder.Eval(Container.DataItem, " Quantity " ) %> </ td >
< td width ="100" > <% # DataBinder.Eval(Container.DataItem, " OrderDate " , " {0:yyyyMMdd} " ) %> </ td >
< td width ="50" >< asp:CheckBox ID ="CheckBox1" runat ="server" Checked ='<%# DataBinder.Eval(Container.DataItem, "IsStored") % > ' /> </ td >
</ tr >
</ tbody >
</ table >
</ ItemTemplate >
</ asp:Repeater >
< br />
< asp:Button ID ="BtnFirst" runat ="server" Text ="首页" OnClick ="BtnFirst_Click" />
< asp:Button ID ="BtnPrev" runat ="server" Text ="上一页" OnClick ="BtnPrev_Click" />
< asp:Button ID ="BtnNext" runat ="server" Text ="下一页" OnClick ="BtnNext_Click" />
< asp:Button ID ="BtnLast" runat ="server" Text ="尾页" OnClick ="BtnLast_Click" />
当前第 < asp:Label ID ="lbCurrentPage" runat ="server" Text ="1" ></ asp:Label > 页/共 < asp:Label ID="lbPageCount" runat="server"></asp:Label>页
共 < asp:Label ID ="lbRecordCount" runat ="server" ></ asp:Label > 记录
<! 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 > PagerDemo 01 </ title >
</ head >
< body >
< form id ="form1" runat ="server" >
< div style ="width: 100%; height: 500px; text-align:center; margin: 0 auto;" >
< asp:Repeater ID ="Repeater1" runat ="server" >
< ItemTemplate >
< table width ="500" style ="margin: 0 auto" >
< tbody >
< tr >
< td width ="50" > <% # DataBinder.Eval(Container.DataItem, " Id " ) %> </ td >
< td > <% # DataBinder.Eval(Container.DataItem, " ProductName " ) %> </ td >
< td width ="50" > <% # DataBinder.Eval(Container.DataItem, " Quantity " ) %> </ td >
< td width ="100" > <% # DataBinder.Eval(Container.DataItem, " OrderDate " , " {0:yyyyMMdd} " ) %> </ td >
< td width ="50" >< asp:CheckBox ID ="CheckBox1" runat ="server" Checked ='<%# DataBinder.Eval(Container.DataItem, "IsStored") % > ' /> </ td >
</ tr >
</ tbody >
</ table >
</ ItemTemplate >
</ asp:Repeater >
< br />
< asp:Button ID ="BtnFirst" runat ="server" Text ="首页" OnClick ="BtnFirst_Click" />
< asp:Button ID ="BtnPrev" runat ="server" Text ="上一页" OnClick ="BtnPrev_Click" />
< asp:Button ID ="BtnNext" runat ="server" Text ="下一页" OnClick ="BtnNext_Click" />
< asp:Button ID ="BtnLast" runat ="server" Text ="尾页" OnClick ="BtnLast_Click" />
当前第 < asp:Label ID ="lbCurrentPage" runat ="server" Text ="1" ></ asp:Label > 页/共 < asp:Label ID="lbPageCount" runat="server"></asp:Label>页
共 < asp:Label ID ="lbRecordCount" runat ="server" ></ asp:Label > 记录
<
asp:DropDownList
ID
="DropDownList1"
runat
="server"
Width
="100px"
OnSelectedIndexChanged
="DropDownList1_SelectedIndexChanged"
AutoPostBack
="True"
></asp:DropDownList>
</ div >
</ form >
</ body >
</ html >
</ div >
</ form >
</ body >
</ html >
后台分页代码
View Code
using
System;
using System.Data;
using System.Configuration;
using System.Web;
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 PagerDemo_PagerDemp01 : System.Web.UI.Page
{
#region * 字段声明 *
// public static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["NorthWindDemoConnectionString"].ConnectionString;
#endregion
protected void Page_Load( object sender, EventArgs e)
{
DisplayData();
}
private void DisplayData()
{
string SqlConnString = ConfigurationManager.ConnectionStrings[ " NorthwindDemoConnectionString " ].ConnectionString;
SqlConnection SqlConn = new SqlConnection(SqlConnString);
SqlConn.Open();
SqlDataAdapter daCmd = new SqlDataAdapter( " SELECT * FROM ProductInfo " , SqlConn);
DataTable dtSouce = new DataTable( " Product " );
daCmd.Fill(dtSouce);
#region * 分页数据源 *
int CurrentPage = Convert.ToInt32(lbCurrentPage.Text);
PagedDataSource pdsPager = new PagedDataSource();
pdsPager.DataSource = dtSouce.DefaultView;
// 允许分页
pdsPager.AllowPaging = true ;
pdsPager.PageSize = 10 ; // 页大小
pdsPager.CurrentPageIndex = CurrentPage - 1 ;
lbPageCount.Text = pdsPager.PageCount.ToString();
lbRecordCount.Text = pdsPager.DataSourceCount.ToString();
if ( ! IsPostBack)
{
for ( int i = 1 ; i <= pdsPager.PageCount; i ++ )
{
ListItem item = new ListItem();
item.Text = " 第 " + i.ToString() + " 页 " ;
item.Value = i.ToString();
DropDownList1.Items.Add(item);
}
DropDownList1.SelectedItem.Text = " 第 " + CurrentPage.ToString() + " 页 " ;
}
BtnFirst.Enabled = true ;
BtnLast.Enabled = true ;
BtnNext.Enabled = true ;
BtnPrev.Enabled = true ;
if (pdsPager.IsLastPage)
{
BtnLast.Enabled = false ;
BtnNext.Enabled = false ;
}
if (pdsPager.IsFirstPage)
{
BtnFirst.Enabled = false ;
BtnPrev.Enabled = false ;
}
Repeater1.DataSource = pdsPager;
Repeater1.DataBind();
#endregion
}
protected void BtnFirst_Click( object sender, EventArgs e)
{
lbCurrentPage.Text = " 1 " ;
DisplayData();
}
protected void BtnPrev_Click( object sender, EventArgs e)
{
lbCurrentPage.Text = Convert.ToString(Convert.ToInt32(lbCurrentPage.Text) - 1 );
DisplayData();
}
protected void BtnNext_Click( object sender, EventArgs e)
{
lbCurrentPage.Text = Convert.ToString(Convert.ToInt32(lbCurrentPage.Text) + 1 );
DisplayData();
}
protected void BtnLast_Click( object sender, EventArgs e)
{
lbCurrentPage.Text = lbPageCount.Text;
DisplayData();
}
protected void DropDownList1_SelectedIndexChanged( object sender, EventArgs e)
{
int page = Convert.ToInt32(DropDownList1.SelectedItem.Value);
lbCurrentPage.Text = page.ToString();
DisplayData();
}
}
using System.Data;
using System.Configuration;
using System.Web;
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 PagerDemo_PagerDemp01 : System.Web.UI.Page
{
#region * 字段声明 *
// public static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["NorthWindDemoConnectionString"].ConnectionString;
#endregion
protected void Page_Load( object sender, EventArgs e)
{
DisplayData();
}
private void DisplayData()
{
string SqlConnString = ConfigurationManager.ConnectionStrings[ " NorthwindDemoConnectionString " ].ConnectionString;
SqlConnection SqlConn = new SqlConnection(SqlConnString);
SqlConn.Open();
SqlDataAdapter daCmd = new SqlDataAdapter( " SELECT * FROM ProductInfo " , SqlConn);
DataTable dtSouce = new DataTable( " Product " );
daCmd.Fill(dtSouce);
#region * 分页数据源 *
int CurrentPage = Convert.ToInt32(lbCurrentPage.Text);
PagedDataSource pdsPager = new PagedDataSource();
pdsPager.DataSource = dtSouce.DefaultView;
// 允许分页
pdsPager.AllowPaging = true ;
pdsPager.PageSize = 10 ; // 页大小
pdsPager.CurrentPageIndex = CurrentPage - 1 ;
lbPageCount.Text = pdsPager.PageCount.ToString();
lbRecordCount.Text = pdsPager.DataSourceCount.ToString();
if ( ! IsPostBack)
{
for ( int i = 1 ; i <= pdsPager.PageCount; i ++ )
{
ListItem item = new ListItem();
item.Text = " 第 " + i.ToString() + " 页 " ;
item.Value = i.ToString();
DropDownList1.Items.Add(item);
}
DropDownList1.SelectedItem.Text = " 第 " + CurrentPage.ToString() + " 页 " ;
}
BtnFirst.Enabled = true ;
BtnLast.Enabled = true ;
BtnNext.Enabled = true ;
BtnPrev.Enabled = true ;
if (pdsPager.IsLastPage)
{
BtnLast.Enabled = false ;
BtnNext.Enabled = false ;
}
if (pdsPager.IsFirstPage)
{
BtnFirst.Enabled = false ;
BtnPrev.Enabled = false ;
}
Repeater1.DataSource = pdsPager;
Repeater1.DataBind();
#endregion
}
protected void BtnFirst_Click( object sender, EventArgs e)
{
lbCurrentPage.Text = " 1 " ;
DisplayData();
}
protected void BtnPrev_Click( object sender, EventArgs e)
{
lbCurrentPage.Text = Convert.ToString(Convert.ToInt32(lbCurrentPage.Text) - 1 );
DisplayData();
}
protected void BtnNext_Click( object sender, EventArgs e)
{
lbCurrentPage.Text = Convert.ToString(Convert.ToInt32(lbCurrentPage.Text) + 1 );
DisplayData();
}
protected void BtnLast_Click( object sender, EventArgs e)
{
lbCurrentPage.Text = lbPageCount.Text;
DisplayData();
}
protected void DropDownList1_SelectedIndexChanged( object sender, EventArgs e)
{
int page = Convert.ToInt32(DropDownList1.SelectedItem.Value);
lbCurrentPage.Text = page.ToString();
DisplayData();
}
}
1、每次在我写分页代码的时候,总是感觉PagedDataSource不能全局调用
2、在上一页、下一页我感觉自己做的还不够好。有时间想想看怎么写能写的更好点