datalist分页(二)

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="datalist3.aspx.cs" Inherits="datalist3" %>

 2 

 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 4 

 5 <html xmlns="http://www.w3.org/1999/xhtml">

 6 <head id="Head1" runat="server">

 7     <title>无标题页</title>

 8 </head>

 9 <body>

10     <form id="form1" runat="server">

11     <div>

12         <TABLE id="Table1" style="Z-INDEX: 101; LEFT: 32px; WIDTH: 752px; POSITION: absolute; TOP: 16px; HEIGHT: 312px" cellSpacing="0" cellPadding="0" width="752" border="0">

13             <TR>

14                 <TD style="HEIGHT: 29px"><FONT face="宋体">DataList分页技术和超级链接</FONT></TD>

15             </TR>

16             <TR>

17                 <TD style="HEIGHT: 252px">

18                 <asp:datalist id="DataList1" runat="server" Width="576px" Height="96px">

19                      <HeaderTemplate>

20                             新闻<td>

21                             

22                      </HeaderTemplate>

23 

24                      <ItemTemplate>

25                         <%# DataBinder.Eval(Container.DataItem, "NewsTitle")%> <td>

26                         

27                      </ItemTemplate>

28                  </asp:datalist>

29                  </TD>

30                 </TR>

31                 <TR style="font-size:12px;">

32                     <TD><FONT    face="宋体">

33 

34             <asp:linkbutton id="FirstLB" Font-Size="12px" runat="server" OnCommand="LinkButton_Click" CommandName="first">第一页</asp:linkbutton>&nbsp;

35             <asp:linkbutton id="PreviousLB" Font-Size="12px" runat="server" OnCommand="LinkButton_Click" CommandName="prev">上一页</asp:linkbutton>&nbsp;

36             <asp:linkbutton id="NextLB" Font-Size="12px" runat="server" OnCommand=LinkButton_Click CommandName="next">下一页</asp:linkbutton>&nbsp;

37             <asp:linkbutton id="EndLB" Font-Size="12px" runat="server" OnCommand=LinkButton_Click CommandName="end">最后一页</asp:linkbutton>&nbsp;&nbsp;

38             总<asp:label id="TotalLbl" Font-Size="12px" runat="server"></asp:label>页 当前第<asp:label id="CurrentLbl" runat="server"></asp:label>39             <asp:linkbutton id="JumpLB" Font-Size="12px" runat="server" OnCommand=LinkButton_Click 

40                             CommandName="jump" onclick="JumpLB_Click">跳到</asp:linkbutton>41             <asp:textbox id="TextBox1" Font-Size="12px" runat="server" Width="90px"></asp:textbox>

42             页</FONT></TD>

43             </TR>

44             </TABLE>

45     </div>

46     </form>

47 </body>

48 </html>
View Code
  1 using System;

  2 using System.Data;

  3 using System.Configuration;

  4 using System.Collections;

  5 using System.Web;

  6 using System.Web.Security;

  7 using System.Web.UI;

  8 using System.Web.UI.WebControls;

  9 using System.Web.UI.WebControls.WebParts;

 10 using System.Web.UI.HtmlControls;

 11 

 12 using System.Data.SqlClient;

 13 

 14 

 15 

 16 

 17 public partial class datalist3 : System.Web.UI.Page

 18 {

 19     int CurrentPage;//当前页数

 20     int PageSize;   //每页条数

 21     int PageCount;  //总页数

 22     int RecordCount;//总条数

 23     private void Page_Load(object sender, System.EventArgs e)

 24     {

 25         // 在此处放置用户代码以初始化页面

 26 

 27 

 28         PageSize = 6;//每页10条记录

 29 

 30 

 31         if (!Page.IsPostBack)

 32         {

 33             CurrentPage = 0;//当前页习惯设为0

 34             ViewState["PageIndex"] = 0;//页索引也设为0

 35 

 36 

 37             //计算总共有多少记录

 38             RecordCount = CalculateRecord();

 39 

 40 

 41             //计算总共有多少页

 42             if (RecordCount % PageSize == 0)

 43             {

 44                 PageCount = RecordCount / PageSize;

 45             }

 46             else

 47             {

 48                 PageCount = RecordCount / PageSize + 1;

 49             }

 50 

 51             this.TotalLbl.Text = PageCount.ToString();//显示总页数

 52             ViewState["PageCount"] = PageCount;//会话session 对整个 application 有效 ,而视图状态 viewstate相当于某个页面的 session

 53 

 54             this.DataListBind();//不可以放在初始化条件之前就绑定,那样的话,如果仅有一页的数据,“下一页”页仍然显示

 55 

 56         }

 57 

 58 

 59     }

 60 

 61 

 62     //计算总共有多少条记录

 63     private int CalculateRecord()

 64     {

 65         try

 66         {

 67             int recordCount;

 68             SqlConnection con = new SqlConnection("server=192.168.10.100;database=Sura;user id=sa;password=13902195880;");//数据库使用Northwind;

 69             con.Open();

 70 

 71             //string sql = "select NewsId,NewsTitle,NewsContents,CreateDate  from tblNews order by CreateDate DESC";

 72             string sql = "select count(*) as count from tblNews";

 73             SqlCommand cmd = new SqlCommand(sql, con);

 74             SqlDataReader sdr = cmd.ExecuteReader();

 75 

 76             if (sdr.Read())

 77             {

 78                 recordCount = Int32.Parse(sdr["count"].ToString());

 79             }

 80 

 81 

 82             else

 83             {

 84                 recordCount = 0;

 85             }

 86 

 87             sdr.Close();

 88             con.Close();

 89             return recordCount;

 90         }

 91 

 92 

 93         catch (Exception ex)

 94         {

 95             throw new Exception(ex.Message);

 96         }

 97     }

 98 

 99 

100     //将数据绑定到Datalist控件

101     public void DataListBind()

102     {

103         try

104         {

105             int StartIndex = CurrentPage * PageSize;//设定导入的起终地址

106             string sql = "select * from tblNews";

107             DataSet ds = new DataSet();

108             SqlConnection con = new SqlConnection("server=192.168.10.100;database=Sura;user id=sa;password=13902195880;");

109             con.Open();

110 

111             SqlDataAdapter sda = new SqlDataAdapter(sql, con);

112             sda.Fill(ds, StartIndex, PageSize, "NewsTitle");//这是sda.Fill方法的第一次重载,里面的变量分别是数据集DataSet ,开始记录数StartRecord,最大的记录数MaxRecord,数据表名TableName

113             this.DataList1.DataSource = ds.Tables["NewsTitle"].DefaultView;

114             this.DataList1.DataBind();

115             this.PreviousLB.Enabled = true;

116             this.NextLB.Enabled = true;

117             if (CurrentPage == (PageCount - 1)) this.NextLB.Enabled = false;//当为最后一页时,下一页链接按钮不可用

118             if (CurrentPage == 0) this.PreviousLB.Enabled = false;//当为第一页时,上一页按钮不可用

119             this.CurrentLbl.Text = (CurrentPage + 1).ToString();//当前页数

120 

121         }

122 

123 

124         catch (Exception ex)

125         {

126             throw new Exception(ex.Message);

127         }

128     }

129 

130 

131 

132 

133     public void LinkButton_Click(Object sender, CommandEventArgs e)//自己编写的按钮点击事件

134     {

135         CurrentPage = (int)ViewState["PageIndex"];//获得当前页索引

136         PageCount = (int)ViewState["PageCount"];//获得总页数

137 

138 

139         string cmd = e.CommandName;

140 

141         //判断cmd,以判定翻页方向

142 

143 

144         switch (cmd)

145         {

146             case "prev"://上一页

147                 if (CurrentPage > 0) CurrentPage--;

148                 break;

149 

150             case "next":

151                 if (CurrentPage < (PageCount - 1)) CurrentPage++;//下一页

152                 break;

153 

154             case "first"://第一页

155                 CurrentPage = 0;

156                 break;

157 

158             case "end"://最后一页

159                 CurrentPage = PageCount - 1;

160                 break;

161 

162             case "jump"://跳转到第几页

163                 if (this.TextBox1.Text.Trim() == "" || Int32.Parse(this.TextBox1.Text.Trim()) > PageCount)//如果输入数字为空或超出范围则返回

164                 {

165                     return;

166                 }

167                 else

168                 {

169                     CurrentPage = Int32.Parse(this.TextBox1.Text.ToString()) - 1;

170                     break;

171                 }

172         }

173         ViewState["PageIndex"] = CurrentPage;//获得当前页

174 

175         this.DataListBind();//重新将DataList绑定到数据库

176 

177 

178     }

179 

180 

181     protected void JumpLB_Click(object sender, EventArgs e)

182     {

183 

184     }

185 }

 

 

你可能感兴趣的:(datalist)