c#中的repeater怎样实现分页呢?
我已经把数据与repeater绑定好了,就是不知道怎样把信息分页1.创建repeater<asp:RepeaterID="rep"runat="server"onitem...
我已经把数据与repeater绑定好了,就是不知道怎样把信息分页
1.创建repeater
<asp:Repeater ID="rep" runat="server" onitemcommand="rep_ItemCommand">
<HeaderTemplate> <table> </HeaderTemplate>
<ItemTemplate>
<tr><td ><asp:LinkButton ID="lj" runat="server" CommandArgument='<%#Eval("id")%>' CommandName="详情" ><%# Eval("信息内容")%></asp:LinkButton></td></tr>
</ItemTemplate>
<FooterTemplate> </table> </FooterTemplate>
</asp:Repeater>
2.数据绑定
string mylj = ConfigurationSettings.AppSettings["mycon"].Trim();
SqlConnection mycon = new SqlConnection(mylj);
mycon.Open();//打开数据库
string sql = "select 信息内容,id from 信息表";//定义连接串
SqlDataAdapter mydatas = new SqlDataAdapter(sql, mycon);
DataSet mydata = new DataSet();
mydatas.Fill(mydata);
this.rep.DataSource = mydata;
this.rep.DataBind();
mycon.Close();//数据绑定 展开
1.创建repeater
<asp:Repeater ID="rep" runat="server" onitemcommand="rep_ItemCommand">
<HeaderTemplate> <table> </HeaderTemplate>
<ItemTemplate>
<tr><td ><asp:LinkButton ID="lj" runat="server" CommandArgument='<%#Eval("id")%>' CommandName="详情" ><%# Eval("信息内容")%></asp:LinkButton></td></tr>
</ItemTemplate>
<FooterTemplate> </table> </FooterTemplate>
</asp:Repeater>
2.数据绑定
string mylj = ConfigurationSettings.AppSettings["mycon"].Trim();
SqlConnection mycon = new SqlConnection(mylj);
mycon.Open();//打开数据库
string sql = "select 信息内容,id from 信息表";//定义连接串
SqlDataAdapter mydatas = new SqlDataAdapter(sql, mycon);
DataSet mydata = new DataSet();
mydatas.Fill(mydata);
this.rep.DataSource = mydata;
this.rep.DataBind();
mycon.Close();//数据绑定 展开
3个回答
展开全部
这里说个比较易懂的分页方法。
repeater.DataSource=一个实现IEnumable的实例数据源。
假设你用的Source是var result=你的source数据,其它都可仿照这个理念。
var totalResults=result.Count;
var currentPage=CurrentPage-->这个参数作为访问器在页面每次点击事件时接收
var perPageItem=自定义当前页面显示多少个,这里定义10;
var currentPageItem=results.Skip((currentPage-1)*perPageItem).Take(perPageItem);
var totalPage=(double)toalResults/perPageItem;
做个for循环输出page。for(int i=1;i<=totalPage;i++)
循环出来的pageLink绑定事件,主要绑定 CurrentPage,然后刷新页面。
repeater.DataSource=一个实现IEnumable的实例数据源。
假设你用的Source是var result=你的source数据,其它都可仿照这个理念。
var totalResults=result.Count;
var currentPage=CurrentPage-->这个参数作为访问器在页面每次点击事件时接收
var perPageItem=自定义当前页面显示多少个,这里定义10;
var currentPageItem=results.Skip((currentPage-1)*perPageItem).Take(perPageItem);
var totalPage=(double)toalResults/perPageItem;
做个for循环输出page。for(int i=1;i<=totalPage;i++)
循环出来的pageLink绑定事件,主要绑定 CurrentPage,然后刷新页面。
更多追问追答
追问
能有一个例子吗?看不大懂
追答
你把你源码贴出来 你怎么绑定数据的 和 用的什么project winform还是web
展开全部
repeater.allowPage=true;
pageSize=10;
pageSize=10;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码:
using System;
using System.Data;
using System.Configuration;
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.SqlClient;
using System.Data.Sql;
public partial class _Default : System.Web.UI.Page
{
private void con()
{
SqlConnection con = new SqlConnection("server='.';database=test;uid=sa;pwd=123456;");
SqlConnection conn = new SqlConnection();
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("select * from news", con);
sda.Fill(ds, "newsTitle");
//SqlDataAdapter sda2 = new SqlDataAdapter("select * from ProspectiveBuyer", con);
// sda2.Fill(ds, "title");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["name"].DefaultView;
//PagedDataSource aa = new PagedDataSource();
pds.AllowPaging = true;//允许分页
pds.PageSize = 8;//单页显示项数
int CurPage;
if (Request.QueryString["Page"] != null)
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage = 1;
pds.CurrentPageIndex = CurPage - 1;
int Count = pds.PageCount;
lblCurrentPage.Text = "当前页:" + CurPage.ToString();
labPage.Text = Count.ToString();
if (!pds.IsFirstPage)
{
this.first.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
this.last.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(Count - 1); ;
up.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
else
{
this.first.Visible = false ;
this.last.Visible = false ;
}
if (!pds.IsLastPage)
{
next.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
}
else
{
this.first.Visible = false;
this.last.Visible = false;
}
Repeater1.DataSource = pds ;
Repeater1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con();
this.first.Visible = true;
this.last.Visible = true;
//this.Repeater1.DataSource = pds();
//this.Repeater1.DataBind();
}
}
}
aspx文件代码:
<table>
<tr ><td class="style1" align ="left" >hehe</td></tr>
<tr ><td class="style1">
<asp:Repeater ID="Repeater1" runat="server" >
<HeaderTemplate ><table><tr><td>头模板</td></tr></HeaderTemplate>
<ItemTemplate ><tr><td ><font color="red" > <%#("timekey")%></font></td></tr></ItemTemplate>
<AlternatingItemTemplate ><tr><td > <a href ='Default.aspx?id=<%#"databaselogid" %>'><%#("SalesAmountQuota")%></a></td></tr></AlternatingItemTemplate>
<FooterTemplate ><tr><td>尾模板</td></tr></table></FooterTemplate>
</asp:Repeater>
</td> </tr>
<tr> <td class="style1">
<asp:HyperLink ID="first" runat="server">首页</asp:HyperLink>
<asp:HyperLink ID="next" runat="server">下一页</asp:HyperLink>
<asp:HyperLink ID="up" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="last" runat="server">末页</asp:HyperLink>
</td></tr>
<tr> <td class="style1">当前页为:<asp:Label ID="lblCurrentPage" runat="server"
Text="Label"></asp:Label>
共<asp:Label ID="labPage" runat="server" Text="Label"></asp:Label>
页</td><td class="style1" style="height: 21px">
<asp:HyperLink ID="first" runat="server">首页</asp:HyperLink>
<asp:HyperLink ID="up" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="next" runat="server">下一页</asp:HyperLink>
<asp:HyperLink ID="last" runat="server">末页</asp:HyperLink>
</td></tr>
</table>
using System;
using System.Data;
using System.Configuration;
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.SqlClient;
using System.Data.Sql;
public partial class _Default : System.Web.UI.Page
{
private void con()
{
SqlConnection con = new SqlConnection("server='.';database=test;uid=sa;pwd=123456;");
SqlConnection conn = new SqlConnection();
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("select * from news", con);
sda.Fill(ds, "newsTitle");
//SqlDataAdapter sda2 = new SqlDataAdapter("select * from ProspectiveBuyer", con);
// sda2.Fill(ds, "title");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["name"].DefaultView;
//PagedDataSource aa = new PagedDataSource();
pds.AllowPaging = true;//允许分页
pds.PageSize = 8;//单页显示项数
int CurPage;
if (Request.QueryString["Page"] != null)
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage = 1;
pds.CurrentPageIndex = CurPage - 1;
int Count = pds.PageCount;
lblCurrentPage.Text = "当前页:" + CurPage.ToString();
labPage.Text = Count.ToString();
if (!pds.IsFirstPage)
{
this.first.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
this.last.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(Count - 1); ;
up.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
else
{
this.first.Visible = false ;
this.last.Visible = false ;
}
if (!pds.IsLastPage)
{
next.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
}
else
{
this.first.Visible = false;
this.last.Visible = false;
}
Repeater1.DataSource = pds ;
Repeater1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con();
this.first.Visible = true;
this.last.Visible = true;
//this.Repeater1.DataSource = pds();
//this.Repeater1.DataBind();
}
}
}
aspx文件代码:
<table>
<tr ><td class="style1" align ="left" >hehe</td></tr>
<tr ><td class="style1">
<asp:Repeater ID="Repeater1" runat="server" >
<HeaderTemplate ><table><tr><td>头模板</td></tr></HeaderTemplate>
<ItemTemplate ><tr><td ><font color="red" > <%#("timekey")%></font></td></tr></ItemTemplate>
<AlternatingItemTemplate ><tr><td > <a href ='Default.aspx?id=<%#"databaselogid" %>'><%#("SalesAmountQuota")%></a></td></tr></AlternatingItemTemplate>
<FooterTemplate ><tr><td>尾模板</td></tr></table></FooterTemplate>
</asp:Repeater>
</td> </tr>
<tr> <td class="style1">
<asp:HyperLink ID="first" runat="server">首页</asp:HyperLink>
<asp:HyperLink ID="next" runat="server">下一页</asp:HyperLink>
<asp:HyperLink ID="up" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="last" runat="server">末页</asp:HyperLink>
</td></tr>
<tr> <td class="style1">当前页为:<asp:Label ID="lblCurrentPage" runat="server"
Text="Label"></asp:Label>
共<asp:Label ID="labPage" runat="server" Text="Label"></asp:Label>
页</td><td class="style1" style="height: 21px">
<asp:HyperLink ID="first" runat="server">首页</asp:HyperLink>
<asp:HyperLink ID="up" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="next" runat="server">下一页</asp:HyperLink>
<asp:HyperLink ID="last" runat="server">末页</asp:HyperLink>
</td></tr>
</table>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询