前台
<form id="Form1" runat= "server" action="">
共有 <asp:Label id= "lblRecordCount" runat= "server" /> 条记录
当前为 <asp:Label id= "lblCurrentPage" ForeColor="red" runat= "server"/> /
<asp:Label id= "lblPageCount" ForeColor= "red" runat= "server" /> 页
<asp:DataList id= "score" runat= "server" HeaderStyle-BackColor= "#aaaadd" AlternatingItemStyle-BackColor= "Gainsboro" EditItemStyle-BackColor= "yellow" >
<ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "id ") %>
<div style="z-index: 101; left: 10px; width: 200px; position: relative; top: 10px; float:left;
height: 240px">
<div style="z-index: 101; left: 0px; width:198px; position: relative; top: 0px;
height: 20px">
<asp:Label ID="imgmsLabel" runat="server" Text='<%# Eval("imgms") %>'></asp:Label><br />
</div>
<div style="z-index: 101; left: 0px; width: 198px; position: relative; top: 0px;
height: 198px">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "~/imgsc/"+Eval("imglj") %>'
Style="position: relative" /></div>
<div style="z-index: 101; left: 0px; width:198px; position: relative; top: 0px;
height: 20px">
商家名称: <asp:Label ID="imgidLabel" runat="server" Text='<%# Eval("imgid") %>'></asp:Label><br /></div>
</div>
</ItemTemplate>
<EditItemStyle BackColor="Yellow" />
<AlternatingItemStyle BackColor="Gainsboro" />
<EditItemTemplate>
dsadsa
</EditItemTemplate>
<HeaderStyle BackColor="#AAAADD" />
<SelectedItemTemplate>
dsadsa
</SelectedItemTemplate>
</asp:DataList>
<asp:LinkButton id= "lbnPrevPage" Text= "上一页" CommandName= "prev" OnCommand= "Page_OnClick" runat= "server" />
<asp:LinkButton id= "lbnNextPage" Text= "下一页" CommandName= "next" OnCommand= "Page_OnClick" runat= "server" />
</form>
</body>
</html>
后台using System;
using System.Data;
using System.Configuration;
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.Collections;
public partial class Default4: System.Web.UI.Page
{
ConnDB db = new ConnDB();
int PageSize, RecordCount, PageCount, CurrentPage;
public void Page_Load(Object src, EventArgs e)
{ //设定PageSize
PageSize = 4; //第一次请求执行
if (!Page.IsPostBack)
{
CurrentPage = 0;
ViewState["PageIndex "] = 0;
//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();
//计算总共有多少页
if ((RecordCount % PageSize) > 0) PageCount = RecordCount / PageSize + 1;
else PageCount = RecordCount / PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount "] = PageCount;
ListBind();
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(1) from img_sp";
intCount = Int32.Parse(db.ReturnDataSet(strCount).Tables[0].Rows[0][0].ToString());
return intCount;
} ICollection CreateSource()
{
int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage * PageSize;
string strSel = "select * from img_sp ";
DataSet ds = new DataSet();
ds = db.ReturnDataSet(strSel);
return ds.Tables[0].DefaultView;
}
public void ListBind()
{
score.DataSource = CreateSource();
score.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
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;
}
ViewState["PageIndex "] = CurrentPage;
ListBind();
}
protected void btnSelect_Click(object sender, EventArgs e)
{ }
}
这个做出来设置的显示每页4个。结果都显示出来了。显示总页数和当前页正确,求解决
<form id="Form1" runat= "server" action="">
共有 <asp:Label id= "lblRecordCount" runat= "server" /> 条记录
当前为 <asp:Label id= "lblCurrentPage" ForeColor="red" runat= "server"/> /
<asp:Label id= "lblPageCount" ForeColor= "red" runat= "server" /> 页
<asp:DataList id= "score" runat= "server" HeaderStyle-BackColor= "#aaaadd" AlternatingItemStyle-BackColor= "Gainsboro" EditItemStyle-BackColor= "yellow" >
<ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "id ") %>
<div style="z-index: 101; left: 10px; width: 200px; position: relative; top: 10px; float:left;
height: 240px">
<div style="z-index: 101; left: 0px; width:198px; position: relative; top: 0px;
height: 20px">
<asp:Label ID="imgmsLabel" runat="server" Text='<%# Eval("imgms") %>'></asp:Label><br />
</div>
<div style="z-index: 101; left: 0px; width: 198px; position: relative; top: 0px;
height: 198px">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "~/imgsc/"+Eval("imglj") %>'
Style="position: relative" /></div>
<div style="z-index: 101; left: 0px; width:198px; position: relative; top: 0px;
height: 20px">
商家名称: <asp:Label ID="imgidLabel" runat="server" Text='<%# Eval("imgid") %>'></asp:Label><br /></div>
</div>
</ItemTemplate>
<EditItemStyle BackColor="Yellow" />
<AlternatingItemStyle BackColor="Gainsboro" />
<EditItemTemplate>
dsadsa
</EditItemTemplate>
<HeaderStyle BackColor="#AAAADD" />
<SelectedItemTemplate>
dsadsa
</SelectedItemTemplate>
</asp:DataList>
<asp:LinkButton id= "lbnPrevPage" Text= "上一页" CommandName= "prev" OnCommand= "Page_OnClick" runat= "server" />
<asp:LinkButton id= "lbnNextPage" Text= "下一页" CommandName= "next" OnCommand= "Page_OnClick" runat= "server" />
</form>
</body>
</html>
后台using System;
using System.Data;
using System.Configuration;
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.Collections;
public partial class Default4: System.Web.UI.Page
{
ConnDB db = new ConnDB();
int PageSize, RecordCount, PageCount, CurrentPage;
public void Page_Load(Object src, EventArgs e)
{ //设定PageSize
PageSize = 4; //第一次请求执行
if (!Page.IsPostBack)
{
CurrentPage = 0;
ViewState["PageIndex "] = 0;
//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();
//计算总共有多少页
if ((RecordCount % PageSize) > 0) PageCount = RecordCount / PageSize + 1;
else PageCount = RecordCount / PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount "] = PageCount;
ListBind();
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(1) from img_sp";
intCount = Int32.Parse(db.ReturnDataSet(strCount).Tables[0].Rows[0][0].ToString());
return intCount;
} ICollection CreateSource()
{
int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage * PageSize;
string strSel = "select * from img_sp ";
DataSet ds = new DataSet();
ds = db.ReturnDataSet(strSel);
return ds.Tables[0].DefaultView;
}
public void ListBind()
{
score.DataSource = CreateSource();
score.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
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;
}
ViewState["PageIndex "] = CurrentPage;
ListBind();
}
protected void btnSelect_Click(object sender, EventArgs e)
{ }
}
这个做出来设置的显示每页4个。结果都显示出来了。显示总页数和当前页正确,求解决
我觉得用分页控件AspNetPager 算了