前台ASPX代码:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<a href="View.aspx?id=<%#Eval("id") %>"><%#Eval("Titel") %></a>
</td>
<td>
<%#Eval("Time").ToString().Substring(0, 9)%></font>
</td>
</tr>
</td>
</table>
</ItemTemplate>
</asp:Repeater>
<table>
<tr><td align="center">
<asp:LinkButton ID="lbtnFirstPage" runat="server" OnClick="lbtnFirstPage_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lbtnpritPage" runat="server" OnClick="lbtnLastPage_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="lbtnNextPage" runat="server" OnClick="lbtnNextPage_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lbtnDownPage" runat="server" OnClick="lbtnFinalPage_Click">尾页</asp:LinkButton>
第<asp:Label ID="labPage" runat="server" Text="Label"></asp:Label>页/共<asp:Label ID="LabCountPage" runat="server" Text="Label"></asp:Label>页 共<asp:Label ID="Total" runat="server" />条新闻
</td></tr>
</table>
后台CS代码:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection Conn = new SqlConnection(sqlcon);
Conn.Open();
SqlCommand Cmd = new SqlCommand("select * from NewClass where id=" + Request.QueryString["id"], Conn);//取出从主页面中的Repeater控件传过来的id值(主页面导航条中的分类id)
SqlDataReader sdr= Cmd.ExecuteReader();
if (Dr.Read())
{
HiddenField1.Value = sdr["id"].ToString();//用HiddenField1控件来存放id的值,pageSource要用到这个值
}
Conn.Close();
if (!IsPostBack)
{
this.labPage.Text = "1";
pageSource();//调用pageSource方法
}
SqlConnection R_Conn = new SqlConnection(sqlcon);
SqlDataAdapter R_sda = new SqlDataAdapter("select top 10 * from NewView order by Time desc", R_Conn);
DataSet R_sda = new DataSet();
R_Da.Fill(R_sda, "NewView");
PagedDataSource Pds = new PagedDataSource();
Pds.DataSource = R_sda.Tables["NewView"].DefaultView;
Pds.AllowPaging = true;
Pds.PageSize = 25;
R_Repeater.DataSource = Pds;
R_Repeater.DataBind();
}
//分页
public void pageSource()
{
SqlConnection Conn = new SqlConnection(sqlcon);
SqlDataAdapter sda = new SqlDataAdapter("select * from NewView where Class_id=" + HiddenField1.Value + "order by id desc", Conn);
DataSet Ds = new DataSet();
Da.Fill(Ds, "NewView");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = Ds.Tables["NewView"].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 10;
Total.Text = pds.Count.ToString();
pds.CurrentPageIndex = Convert.ToInt32(this.labPage.Text) - 1;
Repeater1.DataSource = pds;
LabCountPage.Text = pds.PageCount.ToString();
labPage.Text = (pds.CurrentPageIndex + 1).ToString();
this.lbtnFirstPage.Enabled = true;
this.lbtnLastPage.Enabled = true;
this.lbtnNextPage.Enabled = true;
this.lbtnFinalPage.Enabled = true;
if (pds.CurrentPageIndex < 1)//如果页索引小于1,则首页和上一页按钮不显示
{
this.lbtnFirstPage.Enabled = false;
this.lbtnLastPage.Enabled = false;
}
if (pds.CurrentPageIndex == pds.PageCount - 1)//如果页索引为最后一页,也就是总页数减1,则尾页和下一页按钮不显示
{
this.lbtnNextPage.Enabled = false;
this.lbtnDownPage.Enabled = false;
}
Repeater1.DataBind();
}
protected void lbtnLastPage_Click(object sender, EventArgs e)
{
this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text) - 1);
pageSource();
}
protected void lbtnFirstPage_Click(object sender, EventArgs e)
{
this.labPage.Text = "1";
pageSource();
}
protected void lbtnFinalPage_Click(object sender, EventArgs e)
{
this.labPage.Text = this.LabCountPage.Text;
pageSource();
}
protected void lbtnNextPage_Click(object sender, EventArgs e)
{
this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text) + 1);
pageSource();
}