[转贴]Asp.net2.0 VS 2005下的repeater控件本功能分页实例(共有 条记录 共有几页 当前第 页 首页,上一页,下一页,尾页 DropDownList跳转)
Posted on 2006-12-06 19:42 张明 阅读(36) 评论(0) 编辑 收藏 引用 网摘 所属分类: ASP.NET2.0 、C# 、ADO.NET一、预览效果
二、前台控件呈现部分
<asp:repeater id="LeaveMessage" runat="server" >
<ItemTemplate>
<table width="100%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#D4D0C8">
<tr>
<td width="85%" bgcolor="#FFFAFF"><div align="left"><%#DataBinder.Eval(Container.DataItem, "sNewsTitle")%></div></td>
<td width="15%" bgcolor="#FFFAFF" align="left"><%#DataBinder.Eval(Container.DataItem, "dAddTime")%></td>
</tr>
</table>
<hr size="3px" width="90%"/>
</ItemTemplate>
</asp:repeater>
共有<asp:Literal ID="RecordCount" runat="server"></asp:Literal>条记录
共有<asp:Literal ID="PageCount" runat="server"></asp:Literal>页
当前第<asp:Literal ID="Pageindex" runat="server"></asp:Literal>页
<asp:HyperLink ID="FirstPage" runat="server" Text="首页"></asp:HyperLink>
<asp:HyperLink ID="PrevPage" runat="server" Text="上一页"></asp:HyperLink>
<asp:HyperLink ID="NextPage" runat="server" Text="下一页"></asp:HyperLink>
<asp:HyperLink ID="LastPaeg" runat="server" Text="尾页"></asp:HyperLink>
跳转到<asp:Literal ID="Literal1" runat="server"></asp:Literal>页
<ItemTemplate>
<table width="100%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#D4D0C8">
<tr>
<td width="85%" bgcolor="#FFFAFF"><div align="left"><%#DataBinder.Eval(Container.DataItem, "sNewsTitle")%></div></td>
<td width="15%" bgcolor="#FFFAFF" align="left"><%#DataBinder.Eval(Container.DataItem, "dAddTime")%></td>
</tr>
</table>
<hr size="3px" width="90%"/>
</ItemTemplate>
</asp:repeater>
共有<asp:Literal ID="RecordCount" runat="server"></asp:Literal>条记录
共有<asp:Literal ID="PageCount" runat="server"></asp:Literal>页
当前第<asp:Literal ID="Pageindex" runat="server"></asp:Literal>页
<asp:HyperLink ID="FirstPage" runat="server" Text="首页"></asp:HyperLink>
<asp:HyperLink ID="PrevPage" runat="server" Text="上一页"></asp:HyperLink>
<asp:HyperLink ID="NextPage" runat="server" Text="下一页"></asp:HyperLink>
<asp:HyperLink ID="LastPaeg" runat="server" Text="尾页"></asp:HyperLink>
跳转到<asp:Literal ID="Literal1" runat="server"></asp:Literal>页
三、后置代码部分(CS代码)
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.SqlClient;
using System.Text;
public partial class admin_LeaveMessages : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
NewsBind();
}
}
private void NewsBind()//repeater分页并绑定
{
string SqlStr = "select sNewsTitle,dAddTime from [News] order by dAddTime";
string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionSqlServer"].ToString();
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
SqlDataAdapter Adapter = new SqlDataAdapter(SqlStr, conn);
DataSet ds = new DataSet();
try
{
Adapter.Fill(ds, "testTable");
PagedDataSource objPage = new PagedDataSource();
objPage.DataSource=ds.Tables["testTable"].DefaultView;
objPage.AllowPaging=true;
objPage.PageSize=3;
int CurPage;
if (Request.QueryString["Page"] != null)
{
CurPage = Convert.ToInt32(Request.QueryString["page"]);
}
else
{
CurPage = 1;
}
objPage.CurrentPageIndex = CurPage - 1;
LeaveMessage.DataSource=objPage;//这里更改控件名称
LeaveMessage.DataBind();//这里更改控件名称
RecordCount.Text = objPage.DataSourceCount.ToString();
PageCount.Text = objPage.PageCount.ToString();
Pageindex.Text = CurPage.ToString();
Literal1.Text = PageList(objPage.PageCount, CurPage);
//Literal1.Text = PageList(objPage.PageCount, Pageindex, L_Manage); //带参数的:LManage为参数
FirstPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=1";
PrevPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (CurPage - 1);
NextPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (CurPage + 1);
LastPaeg.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + objPage.PageCount.ToString();
if (CurPage <= 1 && objPage.PageCount <= 1)
{
FirstPage.NavigateUrl = "";
PrevPage.NavigateUrl = "";
NextPage.NavigateUrl = "";
LastPaeg.NavigateUrl = "";
/*
FirstPage.Visible = false;
PrevPage.Visible = false;
NextPage.Visible = false;
LastPaeg.Visible = false;
*/
}
if (CurPage <= 1 && objPage.PageCount > 1)
{
FirstPage.NavigateUrl = "";
PrevPage.NavigateUrl = "";
/*
FirstPage.Visible = false;
PrevPage.Visible = false;
*/
}
if (CurPage >= objPage.PageCount)
{
NextPage.NavigateUrl = "";
LastPaeg.NavigateUrl = "";
/*
NextPage.Visible = false;
LastPaeg.Visible = false;
*/
}
}
catch(Exception error)
{
Response.Write(error.ToString());
}
finally
{
conn.Close();
}
}
private string PageList(int Pagecount, int Pageindex)//private string Jump_List(int Pagecount , int Pageindex , long L_Manage)//带参数的传递
{
StringBuilder sb = new StringBuilder();
//下为带参数的传递
//sb.Append("<select id=\"Page_Jump\" name=\"Page_Jump\" onchange=\"window.location='" + Request.CurrentExecutionFilePath + "?page='+ this.options[this.selectedIndex].value + '&Org_ID=" + L_Manage + "';\">");
//不带参数的传递
sb.Append("<select id=\"Page_Jump\" name=\"Page_Jump\" onchange=\"window.location='" + Request.CurrentExecutionFilePath + "?page='+ this.options[this.selectedIndex].value + '';\">");
for (int i = 1; i <= Pagecount; i++)
{
if (Pageindex == i)
sb.Append("<option value='" + i + "' selected>" + i + "</option>");
else
sb.Append("<option value='" + i + "'>" + i + "</option>");
}
sb.Append("</select>");
return sb.ToString();
}
}
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.SqlClient;
using System.Text;
public partial class admin_LeaveMessages : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
NewsBind();
}
}
private void NewsBind()//repeater分页并绑定
{
string SqlStr = "select sNewsTitle,dAddTime from [News] order by dAddTime";
string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionSqlServer"].ToString();
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
SqlDataAdapter Adapter = new SqlDataAdapter(SqlStr, conn);
DataSet ds = new DataSet();
try
{
Adapter.Fill(ds, "testTable");
PagedDataSource objPage = new PagedDataSource();
objPage.DataSource=ds.Tables["testTable"].DefaultView;
objPage.AllowPaging=true;
objPage.PageSize=3;
int CurPage;
if (Request.QueryString["Page"] != null)
{
CurPage = Convert.ToInt32(Request.QueryString["page"]);
}
else
{
CurPage = 1;
}
objPage.CurrentPageIndex = CurPage - 1;
LeaveMessage.DataSource=objPage;//这里更改控件名称
LeaveMessage.DataBind();//这里更改控件名称
RecordCount.Text = objPage.DataSourceCount.ToString();
PageCount.Text = objPage.PageCount.ToString();
Pageindex.Text = CurPage.ToString();
Literal1.Text = PageList(objPage.PageCount, CurPage);
//Literal1.Text = PageList(objPage.PageCount, Pageindex, L_Manage); //带参数的:LManage为参数
FirstPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=1";
PrevPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (CurPage - 1);
NextPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (CurPage + 1);
LastPaeg.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + objPage.PageCount.ToString();
if (CurPage <= 1 && objPage.PageCount <= 1)
{
FirstPage.NavigateUrl = "";
PrevPage.NavigateUrl = "";
NextPage.NavigateUrl = "";
LastPaeg.NavigateUrl = "";
/*
FirstPage.Visible = false;
PrevPage.Visible = false;
NextPage.Visible = false;
LastPaeg.Visible = false;
*/
}
if (CurPage <= 1 && objPage.PageCount > 1)
{
FirstPage.NavigateUrl = "";
PrevPage.NavigateUrl = "";
/*
FirstPage.Visible = false;
PrevPage.Visible = false;
*/
}
if (CurPage >= objPage.PageCount)
{
NextPage.NavigateUrl = "";
LastPaeg.NavigateUrl = "";
/*
NextPage.Visible = false;
LastPaeg.Visible = false;
*/
}
}
catch(Exception error)
{
Response.Write(error.ToString());
}
finally
{
conn.Close();
}
}
private string PageList(int Pagecount, int Pageindex)//private string Jump_List(int Pagecount , int Pageindex , long L_Manage)//带参数的传递
{
StringBuilder sb = new StringBuilder();
//下为带参数的传递
//sb.Append("<select id=\"Page_Jump\" name=\"Page_Jump\" onchange=\"window.location='" + Request.CurrentExecutionFilePath + "?page='+ this.options[this.selectedIndex].value + '&Org_ID=" + L_Manage + "';\">");
//不带参数的传递
sb.Append("<select id=\"Page_Jump\" name=\"Page_Jump\" onchange=\"window.location='" + Request.CurrentExecutionFilePath + "?page='+ this.options[this.selectedIndex].value + '';\">");
for (int i = 1; i <= Pagecount; i++)
{
if (Pageindex == i)
sb.Append("<option value='" + i + "' selected>" + i + "</option>");
else
sb.Append("<option value='" + i + "'>" + i + "</option>");
}
sb.Append("</select>");
return sb.ToString();
}
}