- 二、Web.config配置
- 1 <appSettings>
- 2 <add key="haikelasiConnectionString" value="Data Source=.;Initial Catalog=haikelasi;Persist Security Info=True;User ID=sa;Password=123456" />
- 3 </appSettings>
- 三、前台控件呈现部分
- 1<asp:repeater id="LeaveMessage" runat="server" >
- 2<ItemTemplate>
- 3<table width="100%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#D4D0C8">
- 4<tr>
- 5<td width="85%" bgcolor="#FFFAFF"><div align="left"><%#DataBinder.Eval(Container.DataItem, "sNewsTitle")%></div></td>
- 6<td width="15%" bgcolor="#FFFAFF" align="left"><%#DataBinder.Eval(Container.DataItem, "dAddTime")%></td>
- 7</tr>
- 8</table>
- 9<hr size="3px" width="90%"/>
- 10</ItemTemplate>
- 11</asp:repeater>
- 12共有<asp:Literal ID="RecordCount" runat="server"></asp:Literal>条记录
- 13共有<asp:Literal ID="PageCount" runat="server"></asp:Literal>页
- 14当前第<asp:Literal ID="Pageindex" runat="server"></asp:Literal>页
- 15<asp:HyperLink ID="FirstPage" runat="server" Text="首页"></asp:HyperLink>
- 16<asp:HyperLink ID="PrevPage" runat="server" Text="上一页"></asp:HyperLink>
- 17<asp:HyperLink ID="NextPage" runat="server" Text="下一页"></asp:HyperLink>
- 18<asp:HyperLink ID="LastPaeg" runat="server" Text="尾页"></asp:HyperLink>
- 19跳转到<asp:Literal ID="Literal1" runat="server"></asp:Literal>页
- 四、后置代码部分(CS代码)
- 1using System;
- 2using System.Data;
- 3using System.Configuration;
- 4using System.Collections;
- 5using System.Web;
- 6using System.Web.Security;
- 7using System.Web.UI;
- 8using System.Web.UI.WebControls;
- 9using System.Web.UI.WebControls.WebParts;
- 10using System.Web.UI.HtmlControls;
- 11using System.Data.SqlClient;
- 12using System.Text;
- 13
- 14public partial class admin_LeaveMessages : System.Web.UI.Page
- 15{
- 16 protected void Page_Load(object sender, EventArgs e)
- 17 {
- 18 if (!Page.IsPostBack)
- 19 {
- 20 NewsBind();
- 21 }
- 22 }
- 23 private void NewsBind()//repeater分页并绑定
- 24 {
- 25 string SqlStr = "select sNewsTitle,dAddTime from [News] order by dAddTime";
- 26 string connectionString = System.Configuration.ConfigurationManager.AppSettings["haikelasiConnectionString"].ToString();
- 27 SqlConnection conn = new SqlConnection(connectionString);
- 28 conn.Open();
- 29 SqlDataAdapter Adapter = new SqlDataAdapter(SqlStr, conn);
- 30 DataSet ds = new DataSet();
- 31 try
- 32 {
- 33 Adapter.Fill(ds, "testTable");
- 34 PagedDataSource objPage = new PagedDataSource();
- 35 objPage.DataSource=ds.Tables["testTable"].DefaultView;
- 36 objPage.AllowPaging=true;
- 37 objPage.PageSize=3;
- 38 int CurPage;
- 39 if (Request.QueryString["Page"] != null)
- 40 {
- 41 CurPage = Convert.ToInt32(Request.QueryString["page"]);
- 42 }
- 43 else
- 44 {
- 45 CurPage = 1;
- 46 }
- 47 objPage.CurrentPageIndex = CurPage - 1;
- 48 LeaveMessage.DataSource=objPage;//这里更改控件名称
- 49 LeaveMessage.DataBind();//这里更改控件名称
- 50 RecordCount.Text = objPage.DataSourceCount.ToString();
- 51 PageCount.Text = objPage.PageCount.ToString();
- 52 Pageindex.Text = CurPage.ToString();
- 53 Literal1.Text = PageList(objPage.PageCount, CurPage);
- 54 //Literal1.Text = PageList(objPage.PageCount, Pageindex, L_Manage); //带参数的:LManage为参数
- 55
- 56
- 57 FirstPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=1";
- 58 PrevPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (CurPage - 1);
- 59 NextPage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (CurPage + 1);
- 60 LastPaeg.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + objPage.PageCount.ToString();
- 61 if (CurPage <= 1 && objPage.PageCount <= 1)
- 62 {
- 63 FirstPage.NavigateUrl = "";
- 64 PrevPage.NavigateUrl = "";
- 65 NextPage.NavigateUrl = "";
- 66 LastPaeg.NavigateUrl = "";
- 67 /**//**//**//*
- 68 FirstPage.Visible = false;
- 69 PrevPage.Visible = false;
- 70 NextPage.Visible = false;
- 71 LastPaeg.Visible = false;
- 72 */
- 73 }
- 74 if (CurPage <= 1 && objPage.PageCount > 1)
- 75 {
- 76 FirstPage.NavigateUrl = "";
- 77 PrevPage.NavigateUrl = "";
- 78 /**//**//**//*
- 79 FirstPage.Visible = false;
- 80 PrevPage.Visible = false;
- 81 */
- 82
- 83
- 84 }
- 85 if (CurPage >= objPage.PageCount)
- 86 {
- 87 NextPage.NavigateUrl = "";
- 88 LastPaeg.NavigateUrl = "";
- 89 /**//**//**//*
- 90 NextPage.Visible = false;
- 91 LastPaeg.Visible = false;
- 92 */
- 93 }
- 94 }
- 95 catch(Exception error)
- 96 {
- 97 Response.Write(error.ToString());
- 98 }
- 99 finally
- 100 {
- 101 conn.Close();
- 102 }
- 103 }
- 104 private string PageList(int Pagecount, int Pageindex)//private string Jump_List(int Pagecount , int Pageindex , long L_Manage)//带参数的传递
- 105 {
- 106 StringBuilder sb = new StringBuilder();
- 107 //下为带参数的传递
- 108 //sb.Append("<select id=\"Page_Jump\" name=\"Page_Jump\" onchange=\"window.location='" + Request.CurrentExecutionFilePath + "?page='+ this.options[this.selectedIndex].value + '&Org_ID=" + L_Manage + "';\">");
- 109 //不带参数的传递
- 110 sb.Append("<select id=\"Page_Jump\" name=\"Page_Jump\" onchange=\"window.location='" + Request.CurrentExecutionFilePath + "?page='+ this.options[this.selectedIndex].value + '';\">");
- 111
- 112 for (int i = 1; i <= Pagecount; i++)
- 113 {
- 114 if (Pageindex == i)
- 115 sb.Append("<option value='" + i + "' selected>" + i + "</option>");
- 116 else
- 117 sb.Append("<option value='" + i + "'>" + i + "</option>");
- 118 }
- 119 sb.Append("</select>");
- 120 return sb.ToString();
- 121 }
- 122
- 123
- 摘自:http://hi.baidu.com/%C1%F7%CB%D5boy/blog/item/ccc0ef0839c1c1a22fddd451.html