.Net源码论坛 [ 繁体中文 ]

返回列表 12345678» / 9
发新话题 回复该主题

本人原创-"DataList分页功能" [复制链接]

楼主
[设计页面中的源代码:]
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>DataList分页功能</title>
  6. </head>
  7. <body>
  8.     <form id="form1" runat="server">
  9.     <div style="text-align: center">
  10.         <div align="center">
  11.         <table border="0" style="width: 1px">
  12.             <tr>
  13.                 <td nowrap="nowrap" style="width: 949px; height: 19px" bgcolor="gray">
  14.                     <div align="center" style="font-weight: bold; font-size: 15px; font-family: 'Courier New'; color: white;">
  15.                         学生寝室管理系统->留言板</div>
  16.                 </td>
  17.             </tr>
  18.             <tr>
  19.                 <td style="height: 33px; width: 949px;">
  20.                     <div align="center">
  21.                         <asp:DataList ID="mydatalist" runat="server" style="font-size: 13px">
  22.                             <ItemTemplate>
  23.                                 <div align="center">
  24.                                     <div align="center">
  25.                                         <div align="center">
  26.                                             <table border="0" style="width: 810px; height: 293px; font-size: 14px;">
  27.                                                 <tr>
  28.                                                     <td style="width: 136px; color: white; font-weight: bold; font-size: 14px; height: 19px;" bgcolor="dimgray" align="center">
  29.                                                         留言标题</td>
  30.                                                     <td align="center" style="width: 1813px; color: white; font-weight: bold; font-size: 14px; height: 19px;" bgcolor="dimgray">
  31.                                                         <%#DataBinder.Eval(Container.DataItem, "title")%></td>
  32.                                                     <td align="left" style="width: 763px; height: 19px;" bgcolor="dimgray">
  33.                                                     </td>
  34.                                                 </tr>
  35.                                                 <tr>
  36.                                                     <td style="width: 136px; height: 228px;">
  37.                                                         <div align="left">
  38.                                                             <table border="0" style="width: 205px">
  39.                                                                 <tr>
  40.                                                                     <td width="216" style="height: 125px">
  41.                                                                         <div align="center">
  42.                                                                             <asp:Image ID="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"userface","images/userface/{0}.gif") %>' /></div>
  43.                                                                     </td>
  44.                                                                 </tr>
  45.                                                                 <tr>
  46.                                                                     <td nowrap="nowrap">
  47.                                                                         <div align="left">
  48.                                                                            <font color="#000033"> 姓名:</font><%#DataBinder.Eval(Container.DataItem, "username")%></div>
  49.                                                                     </td>
  50.                                                                 </tr>
  51.                                                                 <tr>
  52.                                                                     <td nowrap="nowrap">
  53.                                                                         <font color="#000033">身份:</font><%#DataBinder.Eval(Container.DataItem, "usershenfen")%></td>
  54.                                                                 </tr>
  55.                                                                 <tr>
  56.                                                                     <td nowrap="nowrap" style="height: 21px">
  57.                                                                         <div align="left">
  58.                                                                             <font color="#000033">性别:</font><%#DataBinder.Eval(Container.DataItem, "usersex")%></div>
  59.                                                                     </td>
  60.                                                                 </tr>
  61.                                                                 <tr>
  62.                                                                     <td nowrap="nowrap">
  63.                                                                         <div align="left">
  64.                                                                            <font color="#000033"> 地址:</font><%#DataBinder.Eval(Container.DataItem, "useraddress")%></div>
  65.                                                                     </td>
  66.                                                                 </tr>
  67.                                                                 <tr>
  68.                                                                     <td nowrap="nowrap">
  69.                                                                         <div align="left">
  70.                                                                            <font color="#000033"> E-mail:</font><%#DataBinder.Eval(Container.DataItem, "useremail")%></div>
  71.                                                                     </td>
  72.                                                                 </tr>
  73.                                                             </table>
  74.                                                         </div>
  75.                                                     </td>
  76.                                                     <td style="height: 228px" colspan="2">
  77.                                                         <div align="center">
  78.                                                             <div align="center">
  79.                                                                 <div align="center">
  80.                                                                     <div align="left">
  81.                                                                     <asp:Image ID="Image2" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"img","images/emot/{0}.gif") %>' /><br />
  82.                                                                         <%#DataBinder.Eval(Container.DataItem, "content")%></div>
  83.                                                                 </div>
  84.                                                             </div>
  85.                                                         </div>
  86.                                                     </td>
  87.                                                 </tr>
  88.                                                 <tr>
  89.                                                     <td colspan="2" style="height: 19px" align="left">
  90.                                                     <font color="#000033">入学时间:</font><%#DataBinder.Eval(Container.DataItem, "entertime")%> |  <font color="#000033">所在系名:</font><%#DataBinder.Eval(Container.DataItem, "userxiname")%>   |  <font color="#000033">所学专业:</font><%#DataBinder.Eval(Container.DataItem, "userzhuanye")%>  |  <font color="#000033">班级名称:</font><%#DataBinder.Eval(Container.DataItem, "classname")%></td>
  91.                                                     <td style="width: 763px; height: 19px;" align ="left" >
  92.                                                       <font color="#000033">  留言时间:</font><%#DataBinder.Eval(Container.DataItem, "edittime")%> </td>
  93.                                                 </tr>
  94.                                             </table>
  95.                                         </div>
  96.                                     </div>
  97.                                 </div>
  98.                                 <div align="center">
  99.                                     <table border="0" style="width: 808px; font-size: 14px;">
  100.                                         <tr>
  101.                                             <td align="left" style="height: 1px">
  102.                                                 <div align="center">
  103.                                                     <hr color="gray" />
  104.                                                 </div>
  105.                                                  <font color="#000033"> 回复人:</font><%#DataBinder.Eval(Container.DataItem, "rename")%>(管理员)               <font color="#000033">回复时间:</font><%#DataBinder.Eval(Container.DataItem, "replytime")%></td>
  106.                                         </tr>
  107.                                         <tr>
  108.                                             <td nowrap="nowrap">
  109.                                                 <div align="left">
  110.                                                       <font color="#000033">回复内容:<asp:Image ID="Image3" runat="server" ImageAlign="AbsMiddle"
  111.                                                           ImageUrl='<%# DataBinder.Eval(Container.DataItem,"replyimg","images/emot/{0}.gif") %>' /></font><%#DataBinder.Eval(Container.DataItem, "reply")%></div>
  112.                                             </td>
  113.                                         </tr>
  114.                                     </table>
  115.                                 </div>
  116.                             </ItemTemplate>
  117.                         </asp:DataList></div>
  118.                 </td>
  119.             </tr>
  120.             <tr>
  121.                 <td nowrap="nowrap" style="height: 24px; width: 949px; font-weight: bold; color: white; background-color: gray;">
  122.                     <div align="center" style="font-size: 14px">
  123.                         <asp:Label ID="Label1" runat="server" Text="每页显示"></asp:Label><asp:Label ID="Label3"
  124.                             runat="server" ForeColor="Red" Style="font-weight: bold" Text="6"></asp:Label><asp:Label
  125.                                 ID="Label2" runat="server" Text="条信息 共有"></asp:Label><asp:Label ID="lblRecordCount"
  126.                                     runat="server" Font-Bold="True" ForeColor="Red"></asp:Label><asp:Label ID="Label4"
  127.                                         runat="server" Text="条记录 当前是第"></asp:Label><asp:Label ID="Labelye" runat="server"
  128.                                             Font-Bold="True" ForeColor="Red" Text="1"></asp:Label><asp:Label ID="Label6" runat="server"
  129.                                                 Style="font-weight: bold" Text="/"></asp:Label><asp:Label ID="lblPageCount" runat="server"
  130.                                                     Font-Bold="True" ForeColor="Red"></asp:Label><asp:Label ID="Label5" runat="server"
  131.                                                         Text="页"></asp:Label>
  132.                         <asp:Button ID="Button1" runat="server" BackColor="#404040"
  133.                                                             CausesValidation="False" Height="22px" OnClick="Button1_Click" Style="font-weight: bold;
  134.                                                             font-size: 13px; color: white" Text="上一页" Width="59px" />
  135.                         <asp:Button ID="Button2" runat="server" BackColor="#404040" CausesValidation="False"
  136.                             Height="22px" OnClick="Button2_Click" Style="font-weight: bold; font-size: 13px;
  137.                             color: white" Text="下一页" Width="55px" />
  138.                         <asp:Button ID="Button3" runat="server" BackColor="#404040" CausesValidation="False"
  139.                             Height="22px" OnClick="Button3_Click" Style="font-weight: bold; font-size: 13px;
  140.                             color: white" Text="首页" />
  141.                         <asp:Button ID="Button4" runat="server" BackColor="#404040" CausesValidation="False"
  142.                             Height="22px" OnClick="Button4_Click" Style="font-weight: bold; font-size: 13px;
  143.                             color: white" Text="尾页" /></div>
  144.                 </td>
  145.             </tr>
  146.         </table>
  147.     </div>
  148.     </div>
  149.     </form>
  150. </body>
  151. </html>
复制代码
最后编辑一半情弧 最后编辑于 2007-06-11 22:29:14
本主题由 超级管理员 zyw19880202 于 10/13/2014 3:46:30 PM 执行 屏蔽帖子 操作
分享 转发
TOP
沙发

回复: 本人原创-"DataList分页功能"

代码隐藏页中的源代码
  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. using System.Data.SqlClient;//引进连接Sql Server的命名空间
  12. public partial class _Default : System.Web.UI.Page
  13. {
  14.     SqlCommand cmd,cmd1;
  15.     SqlConnection cn;
  16.     string strsql;
  17.     SqlDataAdapter da;
  18.     DataSet ds = new DataSet();
  19.     protected void Page_Load(object sender, EventArgs e)
  20.     {
  21.         if (!this.IsPostBack)
  22.         {
  23.             this.databindlist();
  24.         }
  25.     }
  26.     public void databindlist()     //申请一个方法来绑定数据
  27.     {
  28.       
  29.         cn = sqldata.createcon();//连接数据库,我把数据库封装到“sqldata”类中了
  30.         cn.Open();         //打开连接
  31.         int countpage = Convert.ToInt32(this.Labelye.Text);  //获取当前页面
  32.         strsql = "select * from guestbook  order by id desc "; //Sql查询语句
  33.         da = new SqlDataAdapter (strsql, cn);
  34.         da.Fill(ds, "Result");        //填充数据
  35.         System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();   //封装数据绑定控件类,与分页相关的属性
  36.         ps.DataSource = ds.Tables["Result"].DefaultView;
  37.         ps.AllowPaging = true;    //获取或实置一个植,指示是否在数据绑定控件中启用分页
  38.         ps.PageSize = 6;     //获取或实置每页显示的条数
  39.         ps.CurrentPageIndex = countpage - 1;  //获取或实置当前页的索引
  40.         this.lblPageCount.Text = Convert.ToString(ps.PageCount);  //统计共有多少页
  41.         this.Button1.Enabled = true;
  42.         this.Button2.Enabled = true;
  43.         if (countpage == 1)
  44.         {
  45.             this.Button1.Enabled = false;//如果当前页等于"1",那么"Button1"不可用
  46.         }
  47.         if (countpage == ps.PageCount)
  48.         {
  49.             this.Button2.Enabled = false;//如果当前页等于"尾页",那么"Button2"不可用
  50.         }
  51.         this.mydatalist.DataSource = ps;
  52.         this.mydatalist.DataBind();//将数据源绑定到DataList
  53.         SqlCommand  cmd1 = new SqlCommand ("select count(*) from guestbook", cn);
  54.         this.lblRecordCount.Text = Convert.ToString(cmd1.ExecuteScalar());//统计共有多少条记录
  55.         sqldata.pagecount = Convert.ToString(ps.PageCount);
  56.         cn.Close();//关闭连接
  57.     }
  58.     protected void Button1_Click(object sender, EventArgs e)
  59.     {
  60.         this.Labelye.Text = Convert.ToString(Convert.ToInt32(this.Labelye.Text) - 1); //获取当前的页数
  61.         this.databindlist();
  62.     }
  63.     protected void Button2_Click(object sender, EventArgs e)
  64.     {
  65.         this.Labelye.Text = Convert.ToString(Convert.ToInt32(this.Labelye.Text) + 1); //获取下一页的页数
  66.         this.databindlist();  //绑定数据源
  67.     }
  68.     protected void Button3_Click(object sender, EventArgs e)
  69.     {
  70.         this.Labelye.Text = "1"; //获取首页
  71.         this.databindlist(); //绑定数据源
  72.     }
  73.     protected void Button4_Click(object sender, EventArgs e)
  74.     {
  75.         this.Labelye.Text =sqldata.pagecount; //获取尾页
  76.         this.databindlist();  //绑定数据源
  77.     }
  78. }
复制代码
最后编辑一半情弧 最后编辑于 2007-06-11 22:48:35
TOP
板凳

回复:本人原创-"DataList分页功能"

有什么问题可以先看看上面的说明或大家可以一起讨论
TOP
地板

回复:本人原创-"DataList分页功能"

顶起,我先试试,和我原来用的分页差不多 但是没有用存储过程来的效率高的说
广告位招租...
TOP
五楼

回复:本人原创-"DataList分页功能"

这个是你原创的??。。。 这个方法我用过好多次了。。不过我是抄袭的
我只是民工。
TOP
六楼

回复:本人原创-"DataList分页功能"

谢谢啊
TOP
七楼

回复: 本人原创-"DataList分页功能"

能否把SQLDATA页面源码贴上来!谢谢
TOP
八楼

回复:本人原创-"DataList分页功能"

呵呵问题解决,可以更简单的写!
TOP
九楼

回复:本人原创-"DataList分页功能"

似乎还是一开始就读入整个的表,能不能一开始只读取相应页的内容,比如一开始就读第一页的内容,这样可以减少数据库的读取量,还有就是,不然随着你的数据库的增加性能还是不怎么样!看到了在SQL Server 2005上用RowNumber()函数的分页例子,可以百度一下,呵呵。
TOP
十楼

回复:本人原创-"DataList分页功能"

看看,学习
TOP
发新话题 回复该主题