repeater 分页问题讨论
下面有一个实列,,希望能对大家有所帮助。。
首先在Default.aspx在源中写几个HyperLink控件:
<table>
<tr>
<td align ="center"> 共 <asp:Label ID ="totalpage" runat ="server" BackColor="#FF8080" ></asp:Label> 页 当前为第 <asp:Label ID="currentpage" runat ="server" BackColor="#FF8080" ></asp:Label>页
<asp:HyperLink ID ="firstpage" Text ="首页" runat ="server"></asp:HyperLink>
<asp:HyperLink ID ="prepage" Text ="上一页" runat ="server"></asp:HyperLink>
<asp:HyperLink ID ="nexpage" Text ="下一页" runat ="server"></asp:HyperLink>
<asp:HyperLink ID ="lastpage" Text ="末页" runat ="server"></asp:HyperLink>
</td>
</tr>
</table>
下面是在Default.asp.cs中的代码;
protected void Page_Load(object sender, EventArgs e)
{
Pageing ();
}
protected void Pageing()
{
int pageCount;//总页数
int currentPage;//当前页数
int recordCount;//总行数
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["myConn"]);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT [art_repaly], [art_user_id], [art_title], [art_nums], [art_summaary], [art_source] FROM [db_article] where art_class_id=1", con);
DataSet dst = new DataSet();
SqlDataAdapter dap = new SqlDataAdapter(cmd);
dap.Fill(dst, "db_article");
PagedDataSource pad = new PagedDataSource();
pad.DataSource = dst.Tables[0].DefaultView;
pad.AllowPaging = true;
pad.PageSize = 3;
recordCount = dst.Tables[0].Rows.Count;//得到总行数。。。
pageCount = recordCount / pad.PageSize;//得到总页数。。。
if (recordCount % pad.PageSize > 0)
{
pageCount++;//就是说如果最后一页小于三行时在得到总页数时会少一页。。而这里通此式判断可以加上那一页。。
}
if (Request.QueryString["Page"] != null)
{
try
{
currentPage = Int32.Parse(Request.QueryString["Page"].ToString());
}
catch
{
currentPage = 1;
}
}
else
{
currentPage = 1;
}
pad.CurrentPageIndex = currentPage - 1;//
this.totalpage.Text = pageCount.ToString();
this.currentpage.Text = currentPage.ToString();
if (!pad.IsFirstPage)//如果此时不为第一页。。
{
this.prepage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(currentPage - 1);//上一页
this.firstpage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=1";//转到第一页
}
if (!pad.IsLastPage)//如果此时不为最终后一页。。。
{
this.nexpage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(currentPage + 1);//下一页
this.lastpage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + pageCount.ToString();//转到最后一页
}
this.Repeater1.DataSource = pad;//这两句一定不能少哟。。。
this.Repeater1.DataBind();
}