DataList分页问题 本帖最后由 l277759183 于 2009-07-01 15:51:22 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参考,这是vs2005的:testPage.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="testPage.aspx.cs" Inherits="testPage" %><!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"> <asp:DataList ID="DataList1" runat="server"> <ItemTemplate> <table cellpadding="0" cellspacing="0" style="width: 400px;"> <tr> <td> <asp:Label ID="Label1" runat="server" Font-Size="9pt" Text='<%# Eval("title") %>' ForeColor="Black"></asp:Label></td> </tr> </table> </ItemTemplate> </asp:DataList> <br /> <table cellpadding="0" cellspacing="0"> <tr> <td style="width: 100px"> <asp:Label ID="Label5" runat="server" Font-Size="9pt" Text="当前页为 [ "> </asp:Label><asp:Label ID="Label7" runat="server" Text="1" Font-Size="9pt"></asp:Label> <asp:Label ID="Label3" runat="server" Font-Size="9pt" Text=" ]"></asp:Label></td> <td style="width: 40px"> <asp:LinkButton ID="LinkButton2" runat="server" Font-Size="9pt" Font-Underline="False" ForeColor="Red" OnClick="LinkButton2_Click">首页</asp:LinkButton></td> <td style="width: 30px"> <asp:LinkButton ID="LinkButton3" runat="server" Font-Size="9pt" Font-Underline="False" ForeColor="Red" OnClick="LinkButton3_Click" Width="42px">上一页</asp:LinkButton></td> <td style="width: 48px"> <asp:LinkButton ID="LinkButton4" runat="server" Font-Size="9pt" Font-Underline="False" ForeColor="Red" OnClick="LinkButton4_Click" Width="48px">下一页</asp:LinkButton></td> <td style="width: 49px"> <asp:LinkButton ID="LinkButton5" runat="server" Font-Size="9pt" Font-Underline="False" ForeColor="Red" OnClick="LinkButton5_Click" Width="29px">尾页</asp:LinkButton></td> <td align="center" style="width: 118px"> <asp:Label ID="Label10" runat="server" Text="总页为 [ " Font-Size="9pt" Width="52px"></asp:Label> <asp:Label ID="Label2" runat="server" Font-Size="9pt"></asp:Label> <asp:Label ID="Label4" runat="server" Font-Size="9pt" Text=" ]"></asp:Label></td> </tr> </table> </form></body></html>testPage.aspx.csusing 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;public partial class testPage : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { dlBind(); } } public void dlBind() { int curpage = Convert.ToInt32(this.Label7.Text); PagedDataSource ps = new PagedDataSource(); DataSet ds = bind(); //这里是数据源 ps.DataSource = ds.Tables[0].DefaultView; ps.AllowPaging = true; //是否可以分页 ps.PageSize = 10; //显示的数量 ps.CurrentPageIndex = curpage - 1; //取得当前页的页码 this.LinkButton3.Enabled = true; this.LinkButton4.Enabled = true; this.LinkButton5.Enabled = true; this.LinkButton2.Enabled = true; if (curpage == 1) { this.LinkButton2.Enabled = false;//不显示第一页按钮 this.LinkButton3.Enabled = false;//不显示上一页按钮 } if (curpage == ps.PageCount) { this.LinkButton4.Enabled = false;//不显示下一页 this.LinkButton5.Enabled = false;//不显示最后一页 } this.Label2.Text = Convert.ToString(ps.PageCount); this.DataList1.DataSource = ps; this.DataList1.DataKeyField = "id"; this.DataList1.DataBind(); } protected void LinkButton2_Click(object sender, EventArgs e) //第一页 { this.Label7.Text = "1"; //Label7.Text为当前页 this.dlBind(); } protected void LinkButton3_Click(object sender, EventArgs e) //上一页 { this.Label7.Text = Convert.ToString(Convert.ToInt32(this.Label7.Text) - 1); this.dlBind(); } protected void LinkButton4_Click(object sender, EventArgs e) //下一页 { this.Label7.Text = Convert.ToString(Convert.ToInt32(this.Label7.Text) + 1); this.dlBind(); } protected void LinkButton5_Click(object sender, EventArgs e) //最后一页 { this.Label7.Text = this.Label2.Text; //Label2.Text为总页数 this.dlBind(); } } private void prePagebtn_Click(object sender, System.EventArgs e) { this.prePagebtn.Text=Convert.ToString((Convert.ToInt32(this.currPage.Text)-1)); this.currPage.Text=this.prePagebtn.Text; InitBtn(); } private void nextPagebtn_Click(object sender, System.EventArgs e) { this.nextPagebtn.Text=Convert.ToString((Convert.ToInt32(this.currPage.Text)+1)); this.currPage.Text=this.nextPagebtn.Text; InitBtn(); } 你写的有问题吧!见红字部分,你把button的Text改为数字了,改为private void prePagebtn_Click(object sender, System.EventArgs e) { if(this.currPage.Text > 1) this.currPage.Text=this.currPage.Text -1; InitBtn(); } private void nextPagebtn_Click(object sender, System.EventArgs e) { if(this.currPage.Text < this.totalPage.Text) this.currPage.Text=this.nextPagebtn.Text + 1; InitBtn(); } Sorry,你减1的时候先转化下数字,在转化成字符串赋值 int currPageNum = Convert.ToInt32(this.currPage.Text);if(currPageNum > 1) currPageNum = currPageNum - 1;this.currPage.Text = currPageNum.ToString(); 现在的ASP.NET有什么新技术? 下面代码的前两个参数是什么意思啊 SQL存储过程问题 类库的静态方法 GridView 动态列的超连接 初学ASP.NET,有些问题请教大家 c/s版的im软件再开发一套b/s版的im,并且相互直接可以通许,b/s这里要如何实现,c/s已经实现啦??? 怎样得到用户刚刚提交到数据库中的记录的自动编号ID? 报表在连接完数据库后,需要强行断开或关闭连接吗? 这是因为什么出错的???? 如何实现中英文网站 asp.net WebForm_PostBackOptions 未定义 的问题
testPage.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="testPage.aspx.cs" Inherits="testPage" %><!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">
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<table cellpadding="0" cellspacing="0" style="width: 400px;">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Font-Size="9pt" Text='<%# Eval("title") %>' ForeColor="Black"></asp:Label></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<br />
<table cellpadding="0" cellspacing="0">
<tr>
<td style="width: 100px">
<asp:Label ID="Label5" runat="server" Font-Size="9pt" Text="当前页为 [ ">
</asp:Label><asp:Label ID="Label7" runat="server" Text="1" Font-Size="9pt"></asp:Label>
<asp:Label ID="Label3" runat="server" Font-Size="9pt" Text=" ]"></asp:Label></td>
<td style="width: 40px">
<asp:LinkButton ID="LinkButton2" runat="server" Font-Size="9pt" Font-Underline="False"
ForeColor="Red" OnClick="LinkButton2_Click">首页</asp:LinkButton></td>
<td style="width: 30px">
<asp:LinkButton ID="LinkButton3" runat="server" Font-Size="9pt" Font-Underline="False"
ForeColor="Red" OnClick="LinkButton3_Click" Width="42px">上一页</asp:LinkButton></td>
<td style="width: 48px">
<asp:LinkButton ID="LinkButton4" runat="server" Font-Size="9pt" Font-Underline="False"
ForeColor="Red" OnClick="LinkButton4_Click" Width="48px">下一页</asp:LinkButton></td>
<td style="width: 49px">
<asp:LinkButton ID="LinkButton5" runat="server" Font-Size="9pt" Font-Underline="False"
ForeColor="Red" OnClick="LinkButton5_Click" Width="29px">尾页</asp:LinkButton></td>
<td align="center" style="width: 118px">
<asp:Label ID="Label10" runat="server" Text="总页为 [ " Font-Size="9pt" Width="52px"></asp:Label>
<asp:Label ID="Label2" runat="server" Font-Size="9pt"></asp:Label>
<asp:Label ID="Label4" runat="server" Font-Size="9pt" Text=" ]"></asp:Label></td>
</tr>
</table>
</form>
</body>
</html>testPage.aspx.csusing 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;public partial class testPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dlBind();
}
}
public void dlBind()
{
int curpage = Convert.ToInt32(this.Label7.Text);
PagedDataSource ps = new PagedDataSource();
DataSet ds = bind(); //这里是数据源
ps.DataSource = ds.Tables[0].DefaultView;
ps.AllowPaging = true; //是否可以分页
ps.PageSize = 10; //显示的数量
ps.CurrentPageIndex = curpage - 1; //取得当前页的页码
this.LinkButton3.Enabled = true;
this.LinkButton4.Enabled = true;
this.LinkButton5.Enabled = true;
this.LinkButton2.Enabled = true;
if (curpage == 1)
{
this.LinkButton2.Enabled = false;//不显示第一页按钮
this.LinkButton3.Enabled = false;//不显示上一页按钮
}
if (curpage == ps.PageCount)
{
this.LinkButton4.Enabled = false;//不显示下一页
this.LinkButton5.Enabled = false;//不显示最后一页
}
this.Label2.Text = Convert.ToString(ps.PageCount);
this.DataList1.DataSource = ps;
this.DataList1.DataKeyField = "id";
this.DataList1.DataBind();
}
protected void LinkButton2_Click(object sender, EventArgs e) //第一页
{
this.Label7.Text = "1"; //Label7.Text为当前页
this.dlBind();
}
protected void LinkButton3_Click(object sender, EventArgs e) //上一页
{
this.Label7.Text = Convert.ToString(Convert.ToInt32(this.Label7.Text) - 1);
this.dlBind();
}
protected void LinkButton4_Click(object sender, EventArgs e) //下一页
{
this.Label7.Text = Convert.ToString(Convert.ToInt32(this.Label7.Text) + 1);
this.dlBind();
}
protected void LinkButton5_Click(object sender, EventArgs e) //最后一页
{
this.Label7.Text = this.Label2.Text; //Label2.Text为总页数
this.dlBind();
} }
{
this.prePagebtn.Text=Convert.ToString((Convert.ToInt32(this.currPage.Text)-1));
this.currPage.Text=this.prePagebtn.Text;
InitBtn();
}
private void nextPagebtn_Click(object sender, System.EventArgs e)
{
this.nextPagebtn.Text=Convert.ToString((Convert.ToInt32(this.currPage.Text)+1));
this.currPage.Text=this.nextPagebtn.Text;
InitBtn();
}
你写的有问题吧!见红字部分,你把button的Text改为数字了,改为
private void prePagebtn_Click(object sender, System.EventArgs e)
{
if(this.currPage.Text > 1)
this.currPage.Text=this.currPage.Text -1;
InitBtn();
}
private void nextPagebtn_Click(object sender, System.EventArgs e)
{
if(this.currPage.Text < this.totalPage.Text)
this.currPage.Text=this.nextPagebtn.Text + 1;
InitBtn();
}
int currPageNum = Convert.ToInt32(this.currPage.Text);
if(currPageNum > 1)
currPageNum = currPageNum - 1;
this.currPage.Text = currPageNum.ToString();