一、Datagrid+Sql语句(数据导航条为链接,有跳转到第几页的文本框可输入)
1.webform1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="page.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table align="center">
<tr>
<td style="HEIGHT: 104px" colspan=2><asp:datagrid id="DataGrid1" runat="server" PageSize="5" Font-Size="8pt" Font-Names="Verdana" BorderWidth="1px" BackColor="#FFE0C0" BorderColor="#FFC080" HorizontalAlign="Center" CellPadding="3" AllowPaging="True" Height="152px" Width="700px">
<AlternatingItemStyle BackColor="Linen">
</AlternatingItemStyle>
<ItemStyle VerticalAlign="Bottom">
</ItemStyle>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Bottom" BackColor="#FF8000">
</HeaderStyle>
<FooterStyle HorizontalAlign="Justify" BackColor="White">
</FooterStyle>
<PagerStyle HorizontalAlign="Right" ForeColor="Red" Mode="NumericPages">
</PagerStyle>
</asp:datagrid></td>
</tr>
<tr align="middle">
<td align=left><font size=2>一共</font> <asp:Label ID="lblallcount" Runat=server Font-size="8pt" ForeColor="#0000ff" font-names="verdana"></asp:Label> <font size=2>记录</font>
<font size="2">共</font> <asp:label id="lblpagecount" runat="server" Font-Size="8pt" ForeColor="#0000ff" Font-Names="Verdana"></asp:label> <font size="2">页</font>
<font size="2">第</font> <asp:label id="lblcurrentindex" runat="server" Font-Size="8pt" ForeColor="#0000ff" Font-Names="Verdana"></asp:label> <font size="2">页</font></td>
<td align=right><asp:linkbutton id="first" runat="server" font-size="8pt" ForeColor="#3300ff" CommandArgument="first" Font-Names="Verdana"></asp:linkbutton>
<asp:linkbutton id="prev" Font-Size="8pt" ForeColor="#3300ff" CommandArgument="prev" Runat="server" Font-Names="Verdana"></asp:linkbutton>
<asp:linkbutton id="next" Font-Size="8pt" ForeColor="#3300ff" CommandArgument="next" Runat="server" Font-Names="Verdana"></asp:linkbutton>
<asp:linkbutton id="last" Font-Size="8pt" ForeColor="#3300ff" CommandArgument="last" Runat="server" Font-Names="Verdana"></asp:linkbutton>
<font size="2">跳转到第</font><asp:TextBox ID="to" Runat="server" Columns="1" /><font size="2">页</font><asp:Button ID="go" Text="GO" Runat="server" BackColor="WhiteSmoke" Font-Names="Verdana"/>
</td>
</tr>
</table>
</form>
</body>
</HTML>
2.webform1.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace page
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblpagecount;
protected System.Web.UI.WebControls.Label lblcurrentindex;
protected System.Web.UI.WebControls.LinkButton first;
protected System.Web.UI.WebControls.LinkButton prev;
protected System.Web.UI.WebControls.LinkButton next;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox to;
protected System.Web.UI.WebControls.Button go;
protected System.Web.UI.WebControls.Label lblallcount;
protected System.Web.UI.WebControls.LinkButton last;
void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
first.Text="最首页";
prev.Text="前一页";
next.Text="下一页";
last.Text="最后页";
if(!IsPostBack)
{
BindGrid();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.Page_Changed);
this.first.Click += new System.EventHandler(this.btnClick);
this.prev.Click += new System.EventHandler(this.btnClick);
this.next.Click += new System.EventHandler(this.btnClick);
this.last.Click += new System.EventHandler(this.btnClick);
this.go.Click += new System.EventHandler(this.goClick);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
void Page_Changed(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
BindGrid();
}
void BindGrid()
{
SqlConnection cn=new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=");
cn.Open();
SqlDataAdapter da=new SqlDataAdapter("select * from authors",cn);
DataSet ds=new DataSet();
da.Fill(ds,"authors");
DataGrid1.DataSource=ds.Tables["authors"].DefaultView;
DataGrid1.DataBind();
cn.Close();
showstate();
}
void showstate()
{
SqlConnection cn=new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=");
cn.Open();
SqlDataAdapter da=new SqlDataAdapter("select * from authors",cn);
DataSet ds=new DataSet();
da.Fill(ds,"authors");
DataTable dt=ds.Tables["authors"];
int count=dt.Rows.Count;
lblallcount.Text=count.ToString();
lblpagecount.Text=DataGrid1.PageCount.ToString();
lblcurrentindex.Text=(DataGrid1.CurrentPageIndex+1).ToString();
}
void btnClick(object sender,System.EventArgs e)
{
string arg=((LinkButton)sender).CommandArgument;
switch(arg)
{
case ("first"):
DataGrid1.CurrentPageIndex=0;
break;
case ("prev"):
if(DataGrid1.CurrentPageIndex>0)
DataGrid1.CurrentPageIndex--;
break;
case ("next"):
if(DataGrid1.CurrentPageIndex<(DataGrid1.PageCount-1))
DataGrid1.CurrentPageIndex++;
break;
case ("last"):
DataGrid1.CurrentPageIndex=DataGrid1.PageCount-1;
break;
default:
DataGrid1.CurrentPageIndex=Convert.ToInt32(arg);
break;
}
BindGrid();
}
private void goClick(object sender, System.EventArgs e)
{
if(to.Text.Trim()!="")
{
int index=Int32.Parse(to.Text.Trim())-1;
if(index>=0&&index<DataGrid1.PageCount)
{
DataGrid1.CurrentPageIndex=index;
}
}
BindGrid();
}
}
}
二、动态输出法(页面只有导航按钮<>+<%showdata();%>a绑定输出数据的方法)
1。PortSortCsql.aspx
<%@ Page language="c#" Codebehind="PageSortCSql.aspx.cs" AutoEventWireup="false" Inherits="localhost.PageSort.PageSortCSql" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>用C#和SQL结合进行数据浏览分页 </title>
<LINK href="css/main.css" type="text/css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
</HEAD>
<body MS_POSITIONING="GridLayout">
<TABLE height="958" cellSpacing="0" cellPadding="0" width="63" border="0" ms_2d_layout="TRUE">
<TR vAlign="top">
<TD width="63" height="958">
<form id="form1" method="post" runat="server">
<TABLE height="46" cellSpacing="0" cellPadding="0" width="946" border="0" ms_2d_layout="TRUE">
<TR vAlign="top">
<TD width="2" height="2">
<table id="ShowData" cellSpacing="0" cellPadding="0" align="center" border="0" height="0"
width="0">
<%ShowData();%> <!--输出数据-->
</table>
</TD>
<TD width="662"></TD>
<TD width="284"></TD>
</TR>
<TR vAlign="top">
<TD colSpan="3" height="17"></TD>
</TR>
<TR vAlign="top">
<TD colSpan="2" height="29"></TD>
<TD>
<table align="right" height="28" width="283">
<tr>
<td><%PageLoad_Count();%>; <INPUT id="first" type="button" value=" |< " name="first" runat="server"><!--第一页-->
<INPUT id="prior" type="button" value=" < " name="prior" runat="server"><!--上一页-->
<INPUT id="next" type="button" value=" > " name="next" runat="server"><!--下一页-->
<INPUT id="last" type="button" value=" >| " name="last" runat="server"><!--最后一页-->
</td>
</tr>
</table>
</TD>
</TR>
</TABLE>
</form>
</TD>
</TR>
</TABLE>
</body>
</HTML>
2。PortSortCsql.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace localhost.PageSort
{
/// <summary>
/// PageSortCSql 的摘要说明。
/// </summary>
public class PageSortCSql : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputButton first;
protected System.Web.UI.HtmlControls.HtmlInputButton prior;
protected System.Web.UI.HtmlControls.HtmlInputButton next;
protected System.Web.UI.HtmlControls.HtmlInputButton last;
protected static int CurrentPage = 1;//初始化开始页面
protected static int RowCount = 0 ;//本页有多少条
private static bool IsPrior = false;//有“前一页”
private static bool IsNext = false;//有“下一页”
private static bool IsLast = false;//有“最后一页”
protected static int not_shown_records=0;//计算未显示记录数
private static string startID = "";//设置上一页开始ID
private static string endID = "";//设置下一页结束ID
private static int page_count = 10;//初始化页面记录数
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!IsPostBack)
{
this.CountRecord().ToString();// 记录总数
this.Page_Count().ToString();//分页总数
Init_Brower();//初始化浏览
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.first.ServerClick += new System.EventHandler(this.first_ServerClick);
this.prior.ServerClick += new System.EventHandler(this.prior_ServerClick);
this.next.ServerClick += new System.EventHandler(this.next_ServerClick);
this.last.ServerClick += new System.EventHandler(this.last_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
/// <summary>
/// 显示数据
/// </summary>
protected void ShowData()
{
DataSet ds = new DataSet();//数据集
// ASWBLM.Include.UnionInfo_Provider _uip = new ASWBLM.Include.UnionInfo_Provider();
string vSQL ="";
endID="10";
vSQL = GetSQLCommond(vSQL,startID,endID);
SqlConnection SqlCon;
SqlCon=new SqlConnection("server=localhost;database=chafen;uid=sa;pwd=''");
SqlCon.Open();
SqlDataAdapter da=new SqlDataAdapter(vSQL,SqlCon);
da.Fill(ds,"Table");
// ds = _uip.ShowAllUnionInfo(vSQL);//取得全部数据的数据集
try
{
Response.Write("<p align=center>");
foreach(DataRow dr in ds.Tables["Table"].Rows)
{
Response.Write("<tr align = center onmouseover = /"this.bgColor = '#cccccc'/" onmouseout = /"this.bgColor='';/">");
Response.Write("<td align=/"left/" width=/"60%/"><font color=/"#00309C/">");
Response.Write("<a href=/"UnionInfo_Read.aspx?id="+dr["Id"].ToString()+"/" target=/"_self/">");
Response.Write(dr["学号"].ToString());
Response.Write("</a>");
Response.Write("</td>");
Response.Write("<td align=/"right/">");
Response.Write("<font color=/"#999999/">");
Response.Write("( "+dr["课程号"].ToString()+" )");
Response.Write(" ( 已阅读"+dr["成绩"].ToString()+"次 )");
Response.Write("</font>");
Response.Write("</td>");
Response.Write("</tr>");
}
Response.Write("</p>");
startID = ds.Tables["Table"].Rows[0].ItemArray[0].ToString(); //通过数组,取第一个数据,得到开始号“startID”
RowCount = ds.Tables["Table"].DefaultView.Count;//得到表的行数
endID = ds.Tables["Table"].Rows[RowCount-1].ItemArray[0].ToString();//通过数组,取最后一个数据,得到结束号“endID”
}
catch(SqlException e)
{
Response.Write(e.Message);
}
}
/// <summary>
/// 计算未显示记录数
/// </summary>
/// <returns></returns>
protected void NotShownRecords()
{
not_shown_records = this.CountRecord()/*查询总记录数*/ - (CurrentPage/*当前页*/ - 1) * page_count/*每页记录数*/;
}
/// <summary>
/// 进行输出信息
/// </summary>
protected void PageLoad_Count()
{
this.NotShownRecords();
Response.Write("总共"+this.CountRecord()+"条记录 ");
Response.Write("共有"+this.Page_Count()+"页 ");
Response.Write("第"+CurrentPage.ToString()+"页 ");
Response.Write("本页共有"+RowCount.ToString()+"条记录 ");
}
/// <summary>
/// 获得总记录总数
/// </summary>
/// <returns>时间条件范围内记录总数intCount</returns>
protected int CountRecord()
{
int intCount = 0;
SqlConnection SqlCon;
SqlCon=new SqlConnection("server=localhost;database=chafen;uid=sa;pwd=''");
SqlCon.Open();
//找到条件范围内的记录总数
string strCount = "select count(*) from Score";
//找到符合条件的第一个记录
//string strNum = "select top 1 Id from UnionInfo";
SqlCommand MyComm = new SqlCommand(strCount,SqlCon);
SqlDataReader dr = MyComm.ExecuteReader();//读取数据流
if(dr.Read())
{
intCount = Int32.Parse(dr[0].ToString());
}
else
{
intCount = 0;
}
dr.Close();
SqlCon.Close();
return intCount;
}
/// <summary>
/// 总分页数
/// </summary>
/// <returns>分页总数</returns>
protected int Page_Count()
{
int pageSum = 0;//分页总数
pageSum = this.CountRecord() / page_count; ///记录总数/分页的页数
if ((this.CountRecord() % page_count) > 0) pageSum++;
return pageSum;
}
/// <summary>
/// 取得SQL语句
/// </summary>
/// <param name="vCmd">返回命令行</param>
/// <returns></returns>
private string GetSQLCommond(string vCommond,string startID,string endID)
{
this.NotShownRecords();//执行未显示的行
int EndidPageNum=0;
EndidPageNum=Convert.ToInt32(endID)*CurrentPage;;
vCommond = "select * from (SELECT TOP "+page_count+" {0},{1},{2},{3} FROM";
// (select top"+ Convert.ToInt32(endID)*CurrentPage;
// vCommond+="* from [Score] order by {0} asc) as a order by {0} desc";
vCommond+="(select top ";
vCommond+=EndidPageNum.ToString();
vCommond+="* from [Score] order by {0} asc) as a order by {0} desc) as b order by {0} asc";//见我的blog里的TOP的升序排法
if(IsPrior)//判断“上一页”
{
}
if(IsNext)//判断“下一页”
{
}
if (IsLast)//判断“最后一页”
{
}
vCommond = string.Format(vCommond,"Id","学号","课程号","成绩");//这个是数据表的字段
return vCommond;
}
/// <summary>
/// 输入按钮的状态,进行是否可用
/// </summary>
/// <param name="first">第一页的状态</param>
/// <param name="prior">上一页的状态</param>
/// <param name="next1">下一页的状态</param>
/// <param name="last">最后一页的状态</param>
protected void SetButtonState(bool first_,bool prior_,bool next_,bool last_)
{
if (CurrentPage==1)//到“第一页”
{
first.Disabled = true;//第一页状态
prior.Disabled = true;//上一页状态
next.Disabled = false; //下一页状态
last.Disabled = false; //最后一页状态
}
else if (CurrentPage==this.Page_Count())//到“最后一页”
{
first.Disabled = false;//第一页状态
prior.Disabled = false;//上一页状态
next.Disabled = true; //下一页状态
last.Disabled = true; //最后一页状态
}
else
{
first.Disabled = first_;//第一页状态
prior.Disabled = prior_;//上一页状态
next.Disabled = next_; //下一页状态
last.Disabled = last_; //最后一页状态
}
}
/// <summary>
/// 第一页按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void first_ServerClick(object sender, System.EventArgs e)
{
CurrentPage = 1;
this.SetButtonState(true,true,false,false);
startID = "";
endID = "";
RowCount = '0';
IsLast = false;
IsPrior = false;
IsNext = false;
}
/// <summary>
/// 上一页按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void prior_ServerClick(object sender, System.EventArgs e)
{
if( CurrentPage == 1)//判断“当前页”是否为1
{
this.SetButtonState(true,true,false,false);
}
else
{
CurrentPage=CurrentPage - 1;//“当前页”自减
this.SetButtonState(false,false,false,false);
}
IsPrior = true;
IsNext = false;
IsLast = false;
}
/// <summary>
/// 最后一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void last_ServerClick(object sender, System.EventArgs e)
{
CurrentPage=this.Page_Count();//到最后一页
this.SetButtonState(false,false,true,true);
IsLast = true;
IsPrior = false;
IsNext = false;
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void next_ServerClick(object sender, System.EventArgs e)
{
if(CurrentPage == this.Page_Count())//判断“当前页”是否为“分页总数”
{
this.SetButtonState(false,false,true,true);
}
else
{
CurrentPage=CurrentPage + 1;//“当前页”自加
this.SetButtonState(false,false,false,false);
}
IsNext = true;
IsLast = false;
IsPrior = false;
}
/// <summary>
/// 初始浏览按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Init_Brower()
{
CurrentPage = 1;//肯定是从第一页开始
if ((CurrentPage == 1) && (this.Page_Count() == 1))
{
first.Disabled = true;//第一页状态
prior.Disabled = true;//上一页状态
next.Disabled = true;//下一页状态
last.Disabled = true; //最后一页状态
}
else
{
first.Disabled = true;//第一页状态
prior.Disabled = true;//上一页状态
next.Disabled = false;//下一页状态
last.Disabled = false; //最后一页状态
}
startID = "";//开始号
endID = "";//结束号
IsLast = false;
IsPrior = false;
IsNext = false;
}
}
}
三、Dataset分页
1。PageSortWithDataSet.aspx
<%@ Page language="c#" Codebehind="PageSortWithDataSet.aspx.cs" AutoEventWireup="false" Inherits="localhost.PageSort.PageSortWithDataSet" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>PageSortWithDataSet</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 256px; POSITION: absolute; TOP: 0px" runat="server"
AllowCustomPaging="True">
<PagerStyle Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
<asp:Label id="LabelPageLink" style="Z-INDEX: 102; LEFT: 296px; POSITION: absolute; TOP: 304px"
runat="server">Label</asp:Label>
<asp:DropDownList id="DropDownListPageLink" style="Z-INDEX: 103; LEFT: 512px; POSITION: absolute; TOP: 64px"
runat="server" AutoPostBack="True"></asp:DropDownList></FONT>
</form>
</body>
</HTML>
2。PageSortWithDataSet.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
namespace localhost.PageSort
{
/// <summary>
/// PageSortWithDataSet 的摘要说明。
/// </summary>
public class PageSortWithDataSet : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label LabelPageLink;
protected System.Web.UI.WebControls.DropDownList DropDownListPageLink;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected int pagesize=10;//每页要显示的记录数
protected int absolutepage;//要设定的当前页
int pagecount;//总页数
int rowstart;//数据库中分段记录开始的地方
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
// 在此处放置用户代码以初始化页面
//int pagesize=10;//每页要显示的记录数
absolutepage=1;//要设定的当前页
if(Request["page"]!=null)
{
absolutepage=Int32.Parse(Request["page"]);
}
Bind(absolutepage);
//用来显示每一页的连接,这个可以放到一个label里面
StringBuilder sbPageLink=new StringBuilder();
for(int i=1;i<=pagecount;i++)
{
sbPageLink.Append("--<a href='?page=");
sbPageLink.Append(i);
sbPageLink.Append("'>");
sbPageLink.Append(i);
sbPageLink.Append("</a>--");
}
//在Label里显示页码链接,点击datagrid数据改变为下一页
LabelPageLink.Text=sbPageLink.ToString();
//用来显示每一页的连接,这个可以放到一个DropDownList里面
ArrayList al=new ArrayList();
for(int i=1;i<=pagecount;i++)
{
al.Add(i);
}
// for(int i=0;i<al.Count;i++)
// {
// ListItem altem=al[i] as ListItem;
// DropDownListPageLink.Items.Add(altem);
//
// }
DropDownListPageLink.DataSource=al;
DropDownListPageLink.DataBind();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DropDownListPageLink.SelectedIndexChanged += new System.EventHandler(this.DropDownListPageLink_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DropDownListPageLink_SelectedIndexChanged(object sender, System.EventArgs e)
{
absolutepage=Convert.ToInt32(DropDownListPageLink.SelectedValue);
//int pagesize=10;//每页要显示的记录数
//int absolutepage=1;//要设定的当前页
//计算当前页
Bind(absolutepage);
}
//绑定数据
private void Bind(int absolutepage){
SqlConnection MyConn=new SqlConnection(ConfigurationSettings.AppSettings["strConn"]);
string MySql="select * from Score";
SqlDataAdapter MyDa=new SqlDataAdapter(MySql,MyConn);
DataSet ds=new DataSet();
//计算总记录数
MyDa.Fill(ds,"counts");
int rowcount=ds.Tables[0].Rows.Count;
//计算总页数
int n=rowcount/pagesize;
if(rowcount==pagesize*n)
{
pagecount=n;
}
else
{
pagecount=n+1;
}
//计算记录开始数,也就是分段的起始部分
rowstart=pagesize*(absolutepage-1);
MyDa.Fill(ds,rowstart,pagesize,"dtmemberx");把当前的记录段内的记录填加到DataSet中,然后就把数据绑定到某个容器控件,比
DataGrid1.DataSource=ds.Tables["dtmemberx"].DefaultView;
DataGrid1.DataBind();
}
}
}