ASP.NET(C#)留言板计算总页数的问题
publicpartialclass_Default:System.Web.UI.Page{intpageSize,recordCount,pageCount,curre...
public partial class _Default : System.Web.UI.Page
{
int pageSize, recordCount, pageCount, currentPage;
string conStr;
OleDbConnection ConStr1;
protected void Page_Load(object sender, EventArgs e)
{
if ( Session["UserId"] == null)
Response.Redirect("DengLu.aspx");
pageSize = 3;
// string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("MyData.mdb");
conStr = System.Configuration.ConfigurationSettings.AppSettings["ConStr1"].ToString();
ConStr1 = new OleDbConnection(conStr);
if (!Page.IsPostBack)
{
currentPage = 1; // 初始化当前页为第1页
ViewState["CurrentPage"] = currentPage;
recordCount = CalculateRecord(); // 计算共有多少条记录
ViewState["RecordCount"] = recordCount;
pageCount = recordCount / pageSize; // 计算共有多少页
ViewState["PageCount"] = pageCount;
InitDls();
BindData();
}
}
/// <summary>
/// 该方法计算有多少条留言
/// </summary>
/// <returns>返回留言总数</returns>
public int CalculateRecord()
{
int Count;
string countStr = "Select count(*) as Total from GuestBook";
ConStr1.Open();
OleDbCommand comm = new OleDbCommand(countStr, ConStr1);
OleDbDataReader dr = comm.ExecuteReader();
if (dr.Read())
{
Count = int.Parse(dr["Total"].ToString());
}
else
{
Count = 0;
}
ConStr1.Close();
dr.Close();
return Count;
}
// 绑定数据
public void BindData()
{
int startPage;
startPage = (currentPage - 1) * pageSize;
string selectStr = "Select * from GuestBook order by date DESC";
DataSet ds = new DataSet();
ConStr1.Open();
OleDbDataAdapter da = new OleDbDataAdapter(selectStr, ConStr1);
da.Fill(ds, startPage, pageSize, "Repeater1");
Repeater1.DataSource = ds;
Repeater1.DataMember = "Repeater1";
Repeater1.DataBind();
DangQianYe_Label.Text = (currentPage).ToString();
ZongYe_Label.Text = (pageCount).ToString();
LiuYanShu_Label.Text = recordCount.ToString();
DropDownList1.SelectedIndex = currentPage - 1;
ConStr1.Close();
}
这个是计算那的大概代码,它在LABEL控件那就是不给我显示出来,总是一页,按正常的应该有两页,帮我看看啊,急
原来是我数据库只有5条记录,我让每个页面显示3个,所以怎么算它都算不出2页,(数据库我加到6条以上几能显示出2页或者)那有没有方法改啊,5条或者4条记录也可以显示出3页 展开
{
int pageSize, recordCount, pageCount, currentPage;
string conStr;
OleDbConnection ConStr1;
protected void Page_Load(object sender, EventArgs e)
{
if ( Session["UserId"] == null)
Response.Redirect("DengLu.aspx");
pageSize = 3;
// string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("MyData.mdb");
conStr = System.Configuration.ConfigurationSettings.AppSettings["ConStr1"].ToString();
ConStr1 = new OleDbConnection(conStr);
if (!Page.IsPostBack)
{
currentPage = 1; // 初始化当前页为第1页
ViewState["CurrentPage"] = currentPage;
recordCount = CalculateRecord(); // 计算共有多少条记录
ViewState["RecordCount"] = recordCount;
pageCount = recordCount / pageSize; // 计算共有多少页
ViewState["PageCount"] = pageCount;
InitDls();
BindData();
}
}
/// <summary>
/// 该方法计算有多少条留言
/// </summary>
/// <returns>返回留言总数</returns>
public int CalculateRecord()
{
int Count;
string countStr = "Select count(*) as Total from GuestBook";
ConStr1.Open();
OleDbCommand comm = new OleDbCommand(countStr, ConStr1);
OleDbDataReader dr = comm.ExecuteReader();
if (dr.Read())
{
Count = int.Parse(dr["Total"].ToString());
}
else
{
Count = 0;
}
ConStr1.Close();
dr.Close();
return Count;
}
// 绑定数据
public void BindData()
{
int startPage;
startPage = (currentPage - 1) * pageSize;
string selectStr = "Select * from GuestBook order by date DESC";
DataSet ds = new DataSet();
ConStr1.Open();
OleDbDataAdapter da = new OleDbDataAdapter(selectStr, ConStr1);
da.Fill(ds, startPage, pageSize, "Repeater1");
Repeater1.DataSource = ds;
Repeater1.DataMember = "Repeater1";
Repeater1.DataBind();
DangQianYe_Label.Text = (currentPage).ToString();
ZongYe_Label.Text = (pageCount).ToString();
LiuYanShu_Label.Text = recordCount.ToString();
DropDownList1.SelectedIndex = currentPage - 1;
ConStr1.Close();
}
这个是计算那的大概代码,它在LABEL控件那就是不给我显示出来,总是一页,按正常的应该有两页,帮我看看啊,急
原来是我数据库只有5条记录,我让每个页面显示3个,所以怎么算它都算不出2页,(数据库我加到6条以上几能显示出2页或者)那有没有方法改啊,5条或者4条记录也可以显示出3页 展开
3个回答
展开全部
pageCount = recordCount / pageSize; // 计算共有多少页
你自己算算啊 5/3肯定等于1啊
你要这样(recordCount+pageSize-1)/pageSize
那就ok了。
你自己算算啊 5/3肯定等于1啊
你要这样(recordCount+pageSize-1)/pageSize
那就ok了。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我跟你讲哈,乖一点喔,每句都是真理;
1.现在啥年代了,还用那repeater,商务开发里没有用那玩艺,
2.你这个程序编的,简直还没入门,reapeater的分页方法只有一种,you know?那就是用pageddatasource,不过让我去改造你这程序,实在是太花时间,给个模板你,自己琢磨去吧,
PagedDataSource ps = new PagedDataSource();
……
ps.DataSource = ds.Tables[table].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 3;
ps.CurrentPageIndex = curpage - 1;
然后,
Repeater1.DataSource = ps; //注意呀,这儿不是ds
Repeater1.DataMember = "Repeater1";
Repeater1.DataBind();
1.现在啥年代了,还用那repeater,商务开发里没有用那玩艺,
2.你这个程序编的,简直还没入门,reapeater的分页方法只有一种,you know?那就是用pageddatasource,不过让我去改造你这程序,实在是太花时间,给个模板你,自己琢磨去吧,
PagedDataSource ps = new PagedDataSource();
……
ps.DataSource = ds.Tables[table].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 3;
ps.CurrentPageIndex = curpage - 1;
然后,
Repeater1.DataSource = ps; //注意呀,这儿不是ds
Repeater1.DataMember = "Repeater1";
Repeater1.DataBind();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询