1、RepeaterDemo_Page.aspx前台代码
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table cellspacing="3" cellpadding="3">
<tr>
<th bgcolor="#BBA1E0">表单号</th>
<th bgcolor="#BBA1E0">项目经理</th>
<th bgcolor="#BBA1E0">公司名称</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td ><%#Eval("FormNum") %></td>
<td ><%#Eval("PMName")%></td>
<td ><%#Eval("CompanyName")%></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td bgcolor="#BBA1E0"><%#Eval("FormNum") %></td>
<td bgcolor="#BBA1E0"><%#Eval("PMName")%></td>
<td bgcolor="#BBA1E0"><%#Eval("CompanyName")%></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
<tr><td colspan="3"></td></tr>
</table>
</FooterTemplate>
</asp:Repeater>
<div class="ShowPage-font">
<asp:Label ID="lbl_info" runat="server" Text="第x页,共x页"></asp:Label>
<asp:HyperLink ID="firstPage" runat="server">首页</asp:HyperLink>
<asp:HyperLink ID="lnkUp" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="lnkDown" runat="server">下一页</asp:HyperLink>
<asp:HyperLink ID="lastPage" runat="server">尾页</asp:HyperLink>
<asp:Label ID="lblPages" runat="server">跳转到</asp:Label>
<input type="text" runat="server" id="txtPages" style="width:50px" />
<asp:Label ID="lblPage" runat="server" Text="页"></asp:Label>
<asp:Button ID="GoPage" runat="server" Height="21px" Text="GO" onclick="GoPage_Click"/>
</div>
</div>
</form> </body>
2、后台代码
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient; namespace DataSourceDemo
{
public partial class RepeaterDemo_Page : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowPage();
} }
public DataTable Bind()
{
string str = "select FormNum,PMNameCompanyName from v_Guru_Product_License_Applied ";
return DbHelper.Instance.CreateDataTable(CommandType.Text, str);
} public void ShowPage()
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = Bind().DefaultView;
pds.AllowPaging = true;
pds.PageSize = 10;
int currentPage = Convert.ToInt32(Request["page"]);
pds.CurrentPageIndex = currentPage;//设当前页
if (!pds.IsFirstPage)
{
lnkUp.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage - 1);//上一页
firstPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=0";//首页 }
if (!pds.IsLastPage)
{
lnkDown.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage + 1);//下一页
lastPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (pds.PageCount - 1);//尾页 }
lbl_info.Text = "第" + (currentPage + 1) + "页,共" + pds.PageCount + "页";
Repeater1.DataSource = pds;
Repeater1.DataBind(); } protected void GoPage_Click(object sender, EventArgs e)//跳转页面
{
if (txtPages.Value == "")
{
Response.Redirect("RepeaterDemo_Page.aspx?page=" + 0);
}
else
{
Response.Redirect("RepeaterDemo_Page.aspx?page=" + (Convert.ToInt32(txtPages.Value) - 1));
}
}
}
}
在跳转时不能再用HyperLink控件,否则页面刷新,没法控制输入TextBox的值。