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