如何实现.net2.0中的datalist 分页功能 我不知如何实现.net2.0中的datalist 分页功能(急) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Asp.Net DataList 分页的完整代码cs 代码 private void Page_Load(object sender, System.EventArgs e){// 在此处放置用户代码以初始化页面PageSize = 10;sql="select * from products order by all_time desc ";if(!Page.IsPostBack){//计算总共有多少记录 RecordCount = CalculateRecord(); //计算总共有多少页//取整 PageCount = RecordCount/PageSize; if (RecordCount%PageSize > 0) PageCount = PageCount + 1; lblPageCount.Text = PageCount.ToString(); lblRecordCount.Text = RecordCount.ToString(); ViewState["PageCount"] = PageCount; CurrentPage = 0; ViewState["PageIndex"] = 0; //绑定 ListBind();}}public int CalculateRecord() { int intCount; string strCount = "select count(*) as co from products"; SqlConnection Con=new SqlConnection(data.constr);SqlCommand addCommand=new SqlCommand(strCount,Con);addCommand.Connection.Open();SqlDataReader dr;dr=addCommand.ExecuteReader();if(dr.Read()) { intCount = Int32.Parse(dr["co"].ToString()); } else { intCount = 0; } dr.Close(); return intCount; } ICollection CreateSource() { int StartIndex; //设定导入的起终地址 StartIndex = CurrentPage*PageSize; string strSel = "select * from products"; SqlConnection Con=new SqlConnection(data.constr);DataSet ds = new DataSet(); SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,Con); MyAdapter.Fill(ds,StartIndex,PageSize,"products"); return ds.Tables["products"].DefaultView; } public void ListBind() { dl1.DataSource = CreateSource(); dl1.DataBind(); lbnNextPage.Enabled = true; lbnPrevPage.Enabled = true; if(PageCount==0) { lblCurrentPage.Text = "0"; lbnNextPage.Enabled = false; lbnPrevPage.Enabled = false; } else { if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false; if(CurrentPage==0) lbnPrevPage.Enabled = false; lblCurrentPage.Text = (CurrentPage+1).ToString(); } } public void Page_OnClick(Object sender,CommandEventArgs e) { CurrentPage = (int)ViewState["PageIndex"]; PageCount = (int)ViewState["PageCount"]; string cmd = e.CommandName; //判断cmd,以判定翻页方向 switch(cmd) { case "next": if(CurrentPage<(PageCount-1)) CurrentPage++; break; case "prev": if(CurrentPage>0) CurrentPage--; break;case "first":CurrentPage=0;break;case "last":CurrentPage=PageCount-1;break;} ViewState["PageIndex"] = CurrentPage; ListBind(); } HTML 部分:<TABLE id="Table2" cellSpacing="1" cellPadding="1" width="540" border="0"><TR><TD align="right">共<asp:label id="lblRecordCount" runat="server">Label</asp:label>条记录 共<asp:label id="lblPageCount" runat="server">Label</asp:label>页 当前第<asp:label id="lblCurrentPage" runat="server">Label</asp:label>页 <asp:linkbutton id="lbnFirstPage" runat="server" CommandName="first" OnCommand="Page_OnClick">首页</asp:linkbutton> <asp:linkbutton id="lbnPrevPage" CommandName="prev" OnCommand="Page_OnClick" Runat="server">上一页</asp:linkbutton> <asp:linkbutton id="lbnNextPage" runat="server" CommandName="next" OnCommand="Page_OnClick">下一页</asp:linkbutton> <asp:linkbutton id="lbnLastPage" runat="server" CommandName="last" OnCommand="Page_OnClick">尾页</asp:linkbutton></TD></TR></TABLE> 这不是《亲密接触ASP.NET》里的代码吗?一模一样啊 http://blog.csdn.net/taito/archive/2006/03/27/640558.aspx 用我的这个免费分页控件吧,不需要自己写那么长的代码,而且可以实现通过Url来分页的功能:http://www.webdiyer.com 怎样替换数据库中的自定义标记[url]成html的<a>? 有没有好点的分页代码啊? ◆◇◆◇◆ 请问如何给 TextBox 添加水平滚动条? ◆◇◆◇◆ sql排序问题,同一天时间内根据会员等级排序 如何连接ACCESS数据库? DataGrid怎样捕捉鼠标事件? 我要晕倒了!怎么一个问题N天都没人帮忙解决啊! 生成PNG图片发生“GDI+ 中发生一般性错误” 简单问题 我初学ASP.NET请各位指教 <%#bind("字段1")%>的用法 调查一下:你会递归算法吗?和遍历树型结构吗。如果不会这两个,你的工资会是多少?
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
PageSize = 10;
sql="select * from products order by all_time desc ";
if(!Page.IsPostBack)
{
//计算总共有多少记录
RecordCount = CalculateRecord();
//计算总共有多少页
//取整
PageCount = RecordCount/PageSize;
if (RecordCount%PageSize > 0)
PageCount = PageCount + 1;
lblPageCount.Text = PageCount.ToString();
lblRecordCount.Text = RecordCount.ToString();
ViewState["PageCount"] = PageCount;
CurrentPage = 0;
ViewState["PageIndex"] = 0;
//绑定
ListBind();}
}
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from products";
SqlConnection Con=new SqlConnection(data.constr);
SqlCommand addCommand=new SqlCommand(strCount,Con);
addCommand.Connection.Open();
SqlDataReader dr;
dr=addCommand.ExecuteReader();
if(dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
} ICollection CreateSource()
{ int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = "select * from products";
SqlConnection Con=new SqlConnection(data.constr);
DataSet ds = new DataSet();
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,Con);
MyAdapter.Fill(ds,StartIndex,PageSize,"products");
return ds.Tables["products"].DefaultView;
}
public void ListBind()
{
dl1.DataSource = CreateSource();
dl1.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if(PageCount==0)
{
lblCurrentPage.Text = "0";
lbnNextPage.Enabled = false;
lbnPrevPage.Enabled = false;
}
else
{
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage==0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage+1).ToString();
}
}
public void Page_OnClick(Object sender,CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch(cmd)
{
case "next":
if(CurrentPage<(PageCount-1)) CurrentPage++;
break;
case "prev":
if(CurrentPage>0) CurrentPage--;
break;
case "first":
CurrentPage=0;
break;
case "last":
CurrentPage=PageCount-1;
break;
} ViewState["PageIndex"] = CurrentPage; ListBind(); }
HTML 部分:<TABLE id="Table2" cellSpacing="1" cellPadding="1" width="540" border="0">
<TR>
<TD align="right">共<asp:label id="lblRecordCount" runat="server">Label</asp:label>条记录
共<asp:label id="lblPageCount" runat="server">Label</asp:label>页 当前第<asp:label id="lblCurrentPage" runat="server">Label</asp:label>页
<asp:linkbutton id="lbnFirstPage" runat="server" CommandName="first" OnCommand="Page_OnClick">首页</asp:linkbutton>
<asp:linkbutton id="lbnPrevPage" CommandName="prev" OnCommand="Page_OnClick" Runat="server">上一页</asp:linkbutton>
<asp:linkbutton id="lbnNextPage" runat="server" CommandName="next" OnCommand="Page_OnClick">下一页</asp:linkbutton>
<asp:linkbutton id="lbnLastPage" runat="server" CommandName="last" OnCommand="Page_OnClick">尾页</asp:linkbutton></TD>
</TR>
</TABLE>