I am using the following code for paging inside a repeater. I 开发者_Python百科have the paging size set to 3. If there are exactly 3 speech bubbles - the next button is displayed and when I selected I'm redirected to an empty page. However, if there are 4 sepeech bubbles, everything is fine. Is there a way to make sure that if the page size is 3 - no buttons are displayed? Thanks!
PagedDataSource pagedData = new PagedDataSource();
pagedData.DataSource = ds.Tables[0].DefaultView;
pagedData.AllowPaging = true;
pagedData.PageSize = 3;
pagedData.CurrentPageIndex = pageNum;
Repeater1.DataSource = pagedData;
Repeater1.DataBind();
cmd.Connection.Close();
cmd.Connection.Dispose();
if (pageNum == 0)
{
btnPrev.Visible = false;
}
if (pageNum >= Math.Floor((decimal)ds.Tables[0].Rows.Count / 3))
{
btnNext.Visible = false;
}
}
protected void btnNext_Click(object sender, EventArgs e)
{
// Redirects to next page
Response.Redirect("negativestorydetail.aspx?guid=" + id + "&name=" + name + "&role=" + company_role + "&member=" + mem_id + "&company=" + co_id + "&project=" + proj_name + "&proj_id=" + proj_id + "&tag=" + tag + "&page=" + Convert.ToString(pageNum + 1));
}
protected void btnPrev_Click(object sender, EventArgs e)
{
// Redirects to previous page
Response.Redirect("negativestorydetail.aspx?guid=" + id + "&name=" + name + "&role=" + company_role + "&member=" + mem_id + "&company=" + co_id + "&project=" + proj_name + "&proj_id=" + proj_id + "&tag=" + tag + "&page=" + Convert.ToString(pageNum - 1));
}
This condition doesn't quite work out:
if (pageNum >= Math.Floor((decimal)ds.Tables[0].Rows.Count / 3))
{
btnNext.Visible = false;
}
It is not working correctly in the case of 3 results ( or in general N x page size results). Instead it should be:
if (pageNum >= Math.Ceiling((decimal)(ds.Tables[0].Rows.Count / 3)) -1)
{
btnNext.Visible = false;
}
(The -1 is taking account for your page numbers starting at zero not one)
精彩评论