-----------------后台-------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.OleDb;
public partial class news1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Repeater1.DataSource = pds();
//为Repeater1控件指定数据源
Repeater1.DataBind();
//这个当然是必须的,绑定上去
}
}
private PagedDataSource pds()
{
string connstring = "provider=Microsoft.Jet.OleDb.4.0;" + "Data Source=" + Server.MapPath("admin/runrich.mdb");
//声明一个字符串,后面随时可以用
OleDbConnection con = new OleDbConnection(connstring);
//初始化连接
OleDbDataAdapter sda = new OleDbDataAdapter("SELECT [id], [title], [sendtime] FROM [news] ORDER BY [sendtime] DESC", con);
//初始化一个SqlDataAdapter,并给出查询语句
DataSet ds = new DataSet();
//初始化一个DataSet
sda.Fill(ds, "name");
//将上面查询到的数据填充到name表中
PagedDataSource pds = new PagedDataSource();
//初始化一个PagedDataSource,允许控件分页
pds.DataSource = ds.Tables["name"].DefaultView;
//将上面的ds转换成标准数据视图
pds.AllowPaging = true;
//允许分页
pds.PageSize = 9;
//每页大小为5
pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
//设置当前页
return pds;
//将处理完毕的pds对象发出去
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{//判断当前项是页脚模板
int n = pds().PageCount;//将分页总数赋给变量n
int i = pds().CurrentPageIndex;//将当前分页码赋给i
Label lblpc = (Label)e.Item.FindControl("lblpc");
lblpc.Text = n.ToString();
//找到lblpc这个Label,将总页码赋给他
Label lblp = (Label)e.Item.FindControl("lblp");
lblp.Text = Convert.ToString(pds().CurrentPageIndex + 1);
//找到lblp这个Label,将当前页码赋给他,但是注意,因为页码从0开始,这里要直观的话就得加1
HyperLink hlfir = (HyperLink)e.Item.FindControl("hlfir");
hlfir.NavigateUrl = "?page=0";
HyperLink hlla = (HyperLink)e.Item.FindControl("hlla");
hlla.NavigateUrl = "?page=" + Convert.ToInt32(n - 1);
//找到表示最前页和末页的Label,为他们的NavigateUrl属性赋为第0页和最大页码减1
HyperLink hlp = (HyperLink)e.Item.FindControl("hlp");
HyperLink hln = (HyperLink)e.Item.FindControl("hln");
//找到表示上页和下页这两个控件
if (i <= 0)
{//如果当前页已经是第0页
hlp.Enabled = false;
hlfir.Enabled = false;
hln.Enabled = true;
}
else
{
hlp.NavigateUrl = "?page=" + Convert.ToInt32(i - 1);
}
if (i > n - 2)
{//如果当前项已经是最末页
hln.Enabled = false;
hlla.Enabled = false;
hlp.Enabled = true;
}
else
{
hln.NavigateUrl = "?page=" + Convert.ToInt32(i + 1);
}
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.OleDb;
public partial class news1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Repeater1.DataSource = pds();
//为Repeater1控件指定数据源
Repeater1.DataBind();
//这个当然是必须的,绑定上去
}
}
private PagedDataSource pds()
{
string connstring = "provider=Microsoft.Jet.OleDb.4.0;" + "Data Source=" + Server.MapPath("admin/runrich.mdb");
//声明一个字符串,后面随时可以用
OleDbConnection con = new OleDbConnection(connstring);
//初始化连接
OleDbDataAdapter sda = new OleDbDataAdapter("SELECT [id], [title], [sendtime] FROM [news] ORDER BY [sendtime] DESC", con);
//初始化一个SqlDataAdapter,并给出查询语句
DataSet ds = new DataSet();
//初始化一个DataSet
sda.Fill(ds, "name");
//将上面查询到的数据填充到name表中
PagedDataSource pds = new PagedDataSource();
//初始化一个PagedDataSource,允许控件分页
pds.DataSource = ds.Tables["name"].DefaultView;
//将上面的ds转换成标准数据视图
pds.AllowPaging = true;
//允许分页
pds.PageSize = 9;
//每页大小为5
pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
//设置当前页
return pds;
//将处理完毕的pds对象发出去
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{//判断当前项是页脚模板
int n = pds().PageCount;//将分页总数赋给变量n
int i = pds().CurrentPageIndex;//将当前分页码赋给i
Label lblpc = (Label)e.Item.FindControl("lblpc");
lblpc.Text = n.ToString();
//找到lblpc这个Label,将总页码赋给他
Label lblp = (Label)e.Item.FindControl("lblp");
lblp.Text = Convert.ToString(pds().CurrentPageIndex + 1);
//找到lblp这个Label,将当前页码赋给他,但是注意,因为页码从0开始,这里要直观的话就得加1
HyperLink hlfir = (HyperLink)e.Item.FindControl("hlfir");
hlfir.NavigateUrl = "?page=0";
HyperLink hlla = (HyperLink)e.Item.FindControl("hlla");
hlla.NavigateUrl = "?page=" + Convert.ToInt32(n - 1);
//找到表示最前页和末页的Label,为他们的NavigateUrl属性赋为第0页和最大页码减1
HyperLink hlp = (HyperLink)e.Item.FindControl("hlp");
HyperLink hln = (HyperLink)e.Item.FindControl("hln");
//找到表示上页和下页这两个控件
if (i <= 0)
{//如果当前页已经是第0页
hlp.Enabled = false;
hlfir.Enabled = false;
hln.Enabled = true;
}
else
{
hlp.NavigateUrl = "?page=" + Convert.ToInt32(i - 1);
}
if (i > n - 2)
{//如果当前项已经是最末页
hln.Enabled = false;
hlla.Enabled = false;
hlp.Enabled = true;
}
else
{
hln.NavigateUrl = "?page=" + Convert.ToInt32(i + 1);
}
}
}
}
---------------------前台----------------
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
<div id="newstitle">
<span class="text">动态新闻</span>
<span class="link"><a href="news.aspx">……更多</a></span>
</div>
</HeaderTemplate>
<ItemTemplate>
<div id="newsItem">
<span class="newspic"><asp:Image ID="Image1" ImageUrl="~/img/direction.jpg" runat="server"/></span>
<span class="newsTitle"><a href='newshow.aspx?id=<%# Eval("id") %>'><%#Eval("Title")%></a></span>
<span class="newsDate"><%#Eval("sendtime", "{0:d}")%></span>
</div>
</ItemTemplate>
<SeparatorTemplate><div id="pic"><asp:Image ID="Image2" ImageUrl="~/img/newsline.jpg" runat="server"/></div></SeparatorTemplate>
<FooterTemplate><div id="pic1"><asp:Image ID="Image2" ImageUrl="~/img/newsline.jpg" runat="server"/></div><div id="newsfooter"> 共 <asp:Label ID="lblpc" runat="server" Text="Label"></asp:Label> 页 当前为第 <asp:Label ID="lblp" runat="server" Text="Label"></asp:Label> 页
<span class="hyperlink"><asp:HyperLink ID="hlfir" runat="server" Text="首页"/>
<asp:HyperLink ID="hlp" runat="server" Text="上一页"/>
<asp:HyperLink ID="hln" runat="server" Text="下一页"/>
<asp:HyperLink ID="hlla" runat="server" Text="末页"/></span></div></FooterTemplate>
</asp:Repeater>
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
<div id="newstitle">
<span class="text">动态新闻</span>
<span class="link"><a href="news.aspx">……更多</a></span>
</div>
</HeaderTemplate>
<ItemTemplate>
<div id="newsItem">
<span class="newspic"><asp:Image ID="Image1" ImageUrl="~/img/direction.jpg" runat="server"/></span>
<span class="newsTitle"><a href='newshow.aspx?id=<%# Eval("id") %>'><%#Eval("Title")%></a></span>
<span class="newsDate"><%#Eval("sendtime", "{0:d}")%></span>
</div>
</ItemTemplate>
<SeparatorTemplate><div id="pic"><asp:Image ID="Image2" ImageUrl="~/img/newsline.jpg" runat="server"/></div></SeparatorTemplate>
<FooterTemplate><div id="pic1"><asp:Image ID="Image2" ImageUrl="~/img/newsline.jpg" runat="server"/></div><div id="newsfooter"> 共 <asp:Label ID="lblpc" runat="server" Text="Label"></asp:Label> 页 当前为第 <asp:Label ID="lblp" runat="server" Text="Label"></asp:Label> 页
<span class="hyperlink"><asp:HyperLink ID="hlfir" runat="server" Text="首页"/>
<asp:HyperLink ID="hlp" runat="server" Text="上一页"/>
<asp:HyperLink ID="hln" runat="server" Text="下一页"/>
<asp:HyperLink ID="hlla" runat="server" Text="末页"/></span></div></FooterTemplate>
</asp:Repeater>