码迷,mamicode.com
首页 > 其他好文 > 详细

Repeater控件-实现分页(升级版)

时间:2015-02-08 15:27:40      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:

 

原来已经写了一个repeater控件的分页,今天有些了一个优化的程序。

1.解决了当数据条数,小于要显示的数据条数时,出现上一页按钮和还能继续递减的问题。

2.同时,还添加了在显示数据的表中进行删除和修改数据。

技术分享

 

3.还解决了页面间的传值问题,(当点击Edit按钮时,页面跳转到另一个界面,也就是修改信息界面,然后进行修改,修改完成后保存并返回主界面。)

技术分享

以上就是,新增的内容。

 

下面是代码:

 

总计使用了两个页面,一个是显示数据的页面(Default.aspx)另一个是修改信息的页面(Edit.aspx):

 

首先是Default.aspx页面的前台代码:

技术分享
  1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug ="true"%>
  2 
  3 <!DOCTYPE html>
  4 
  5 <html xmlns="http://www.w3.org/1999/xhtml">
  6 <head runat="server">
  7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  8     <title></title>
  9 </head>
 10 <body>
 11     <form id="form1" runat="server">
 12     <div>
 13     <h1>Repeater分页</h1>
 14 
 15         <asp:Repeater ID="Repeater1" runat="server" OnItemCommand ="Repeater1_ItemCommand">
 16         <HeaderTemplate>
 17                 <table>
 18                 <tr><td colspan="7" style="text-align:center; background-color:#76a0ae; color:#ffffff; height:20px">员工信息</td></tr>
 19                 <tr style="color:#1e486e">
 20                 <td>姓名</td>
 21                 <td>年龄</td>
 22                 <td>性别</td>
 23                 <td>工作</td>
 24                 <td colspan="2">操作</td>
 25                 </tr>
 26         </HeaderTemplate>
 27         <ItemTemplate>
 28                 <tr>
 29                     <asp:HiddenField ID="hfId" Value=<%#Eval("PID")%> runat="server" />
 30                     <td class="tdleft">
 31                         <%#Eval("PName")%>
 32                     </td>
 33 
 34                     <td>
 35                         <%#Eval("PAge") %>
 36                     </td>
 37 
 38                     <td>
 39                         <%#Eval("PSex") %>
 40                     </td>
 41 
 42                     <td>
 43                         <%#Eval("PJob") %>
 44                     </td>
 45 
 46                     <td>
 47                         <asp:LinkButton ID="lnkEdit" CommandName="EditData" runat="server">Edit</asp:LinkButton>
 48                     </td>
 49 
 50                     <td>
 51                         <asp:LinkButton ID="lnkDelete" CommandName="DelData" runat="server">Delete</asp:LinkButton>
 52                     </td>
 53 
 54                 </tr>
 55         </ItemTemplate>
 56         <AlternatingItemTemplate>
 57                 <tr style=" background-color:#f0f5f8">
 58                         <asp:HiddenField ID="hfId" Value=<%#Eval("PID")%> runat="server" />
 59                     <td class="tdleft">
 60                         <%#Eval("PName")%>
 61                     </td>
 62 
 63                     <td>
 64                         <%#Eval("PAge") %>
 65                     </td>
 66 
 67                     <td>
 68                         <%#Eval("PSex") %>
 69                     </td>
 70 
 71                     <td>
 72                         <%#Eval("PJob") %>
 73                     </td>
 74 
 75                     <td>
 76                         <asp:LinkButton ID="lnkEdit" CommandName="EditData" runat="server">Edit</asp:LinkButton>
 77                     </td>
 78 
 79                     <td>
 80                         <asp:LinkButton ID="lnkDelete" CommandName="DelData" runat="server">Delete</asp:LinkButton>
 81                     </td>
 82 
 83                 </tr>
 84         </AlternatingItemTemplate>
 85         <FooterTemplate>
 86                         <tr id="noData" runat="server" Visible="<%#Repeater1.Items.Count==0 %>"  >
 87                             <td colspan="7">
 88                                 <div>
 89                                     <p style ="text-align:center">没有相关的数据</p>
 90                                 </div>
 91                             </td>
 92                     </tr>
 93                 </table>
 94         </FooterTemplate>
 95         </asp:Repeater>
 96 
 97         <div id="fenye">
 98             <asp:Label ID="lbNow" runat="server" Text="当前页"></asp:Label>
 99             <asp:Label ID="lbPage" runat="server" Text="1"></asp:Label>
100             <asp:Label ID="lbAll" runat="server" Text="总页数"></asp:Label>
101             <asp:Label ID="lbCount" runat="server" Text=""></asp:Label>
102             <asp:LinkButton ID="lbtnFirst" runat="server" onclick ="lbtnFirst_Click">首页</asp:LinkButton> 
103             <asp:LinkButton ID="lbtnUp" runat="server" onclick ="lbtnUp_Click">上一页</asp:LinkButton> 
104             <asp:LinkButton ID="lbtnDown" runat="server" onclick ="lbtnDown_Click">下一页</asp:LinkButton> 
105             <asp:LinkButton ID="lbtnLast" runat="server" onclick ="lbtnLast_Click">尾页</asp:LinkButton> 
106             <asp:DropDownList ID="DropDownList1" runat="server" Width="80px">
107             </asp:DropDownList> 
108             <asp:LinkButton ID="lbtnGo" runat="server"  BackColor="LightBlue"
109                 BorderWidth="2px" BorderColor="Blue" onclick ="lbtnGo_Click" style="width: 20px">Go</asp:LinkButton>
110         </div>
111         <br />
112     </div>
113 
114     </form>
115 </body>
116 </html>
View Code

 

然后是Default.aspx的后台代码:

技术分享
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Web.UI;
  6 using System.Web.UI.WebControls;
  7 
  8 using System.Data;
  9 using System.Data.SqlClient;
 10 using System.Configuration;
 11 
 12 public partial class _Default : System.Web.UI.Page
 13 {
 14     SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["LinqTestConnectionString"].ToString());
 15 
 16     public SqlCommand sqlCmd = null;
 17 
 18     int countPage = 20;//每一页有多少条数据
 19 
 20     protected void Page_Load(object sender, EventArgs e)
 21     {
 22         if (!IsPostBack)
 23         {
 24             DropListBind();//为DropDownList赋值
 25             Show();//初始化显示第一页,默认当前为第一页
 26             State();//初始化导航按钮的使用状态
 27         }
 28     }
 29 
 30     /// <summary>
 31     /// 第一页
 32     /// </summary>
 33     /// <param name="sender"></param>
 34     /// <param name="e"></param>
 35     protected void lbtnFirst_Click(object sender, EventArgs e)
 36     {
 37         lbPage.Text = "1";
 38         Show();
 39         State();
 40     }
 41 
 42     /// <summary>
 43     /// 上一页
 44     /// </summary>
 45     /// <param name="sender"></param>
 46     /// <param name="e"></param>
 47     protected void lbtnUp_Click(object sender, EventArgs e)
 48     {
 49         lbPage.Text = (Convert.ToInt32(lbPage.Text) - 1).ToString();
 50         Show();
 51         State();
 52     }
 53 
 54     /// <summary>
 55     /// 下一页
 56     /// </summary>
 57     /// <param name="sender"></param>
 58     /// <param name="e"></param>
 59     protected void lbtnDown_Click(object sender, EventArgs e)
 60     {
 61         lbPage.Text = (Convert.ToInt32(lbPage.Text) + 1).ToString();
 62         Show();
 63         State();
 64     }
 65 
 66     /// <summary>
 67     /// 最后一页
 68     /// </summary>
 69     /// <param name="sender"></param>
 70     /// <param name="e"></param>
 71     protected void lbtnLast_Click(object sender, EventArgs e)
 72     {
 73         lbPage.Text = lbCount.Text;
 74         Show();
 75         State();
 76     }
 77 
 78     /// <summary>
 79     /// GO按钮
 80     /// </summary>
 81     /// <param name="sender"></param>
 82     /// <param name="e"></param>
 83     protected void lbtnGo_Click(object sender, EventArgs e)
 84     {
 85         lbPage.Text = DropDownList1.SelectedValue;
 86         Show();
 87         State();
 88     }
 89 
 90     //绑定DropDwonList控件
 91     public void DropListBind()
 92     {
 93         sqlConn.Open();
 94 
 95         sqlCmd = new SqlCommand("select count(*) from Person", sqlConn);//获取数据库中信息的总条数
 96 
 97         int page = Convert.ToInt32(sqlCmd.ExecuteScalar());
 98 
 99         //如果当前页1,且数据库中的数据行小于每一页要显示的数据行,则Label控件全部设置为不可用。
100         if (Convert.ToInt32(lbPage.Text) == 1 && (page - countPage) < 0)
101         {
102             lbtnFirst.Enabled = false;
103             lbtnUp.Enabled = false;
104             lbtnLast.Enabled = false;
105             lbtnDown.Enabled = false;
106         }
107 
108         //每页显示countPage条,算出总页数,并为DropDownList赋值
109 
110         //使用ceiling(天花板函数)--MSDN示例
111         /*
112             *        double[] values = {7.03, 7.64, 0.12, -0.12, -7.1, -7.6};
113             *        Console.WriteLine("  Value          Ceiling          Floor\n");
114             *        foreach (double value in values)
115             *          {
116             *              Console.WriteLine("{0,7} {1,16} {2,14}", value, Math.Ceiling(value), Math.Floor(value));
117             *          }
118             *        The example displays the following output to the console:
119             *                Value          Ceiling          Floor
120             *                 7.03                8              7
121             *                 7.64                8              7
122             *                 0.12                1              0
123             *                -0.12                0             -1
124             *                -7.1                -7             -8
125             *                -7.6                -7             -8
126             */
127         //为什么Page要乘以1.0,因为如果不乘的话,就会默认转换成整型,得不到小数,就无法使用ceiling方法了。
128         this.lbCount.Text = (Math.Ceiling(((page * 1.0 / countPage)))).ToString();
129 
130         int[] num = new int[Convert.ToInt32(lbCount.Text)];
131 
132         for (int i = 1; i <= Convert.ToInt32(lbCount.Text); i++)//如果使用 i= 0,那么在前台显示的时候买第一个值是0.
133         {
134             num[i - 1] = i;
135         }
136 
137         sqlConn.Close();
138 
139         DropDownList1.DataSource = num;
140 
141         DropDownList1.DataBind();
142     }
143 
144     /// <summary>
145     /// 状态设置
146     /// </summary>
147     public void State()
148     {
149         if (lbPage.Text == "1")//如果当前页为第一页,则前一页和首页按钮禁用
150         {
151             lbtnFirst.Enabled = false;
152             lbtnUp.Enabled = false;
153             lbtnLast.Enabled = true;
154             lbtnDown.Enabled = true;
155         }
156 
157         if (lbPage.Text == lbCount.Text)//如果当前页为最后一页,则后一页和尾页按钮禁用
158         {
159             lbtnFirst.Enabled = true;
160             lbtnUp.Enabled = true;
161             lbtnLast.Enabled = false;
162             lbtnDown.Enabled = false;
163         }
164         if (Convert.ToInt32(lbPage.Text) > 1 && Convert.ToInt32(lbPage.Text) < Convert.ToInt32(lbCount.Text))//如果当前也在首页和尾页之间则四个按钮均可用
165         {
166             lbtnFirst.Enabled = true;
167             lbtnUp.Enabled = true;
168             lbtnLast.Enabled = true;
169             lbtnDown.Enabled = true;
170         }
171     }
172 
173     /// <summary>
174     /// 显示数据,绑定数据
175     /// </summary>
176     public void Show()
177     {
178         //从数据库中筛选信息,仅加载当前请求的那一页的信息,效率会相对比较高,并非全部加载
179         
180         
181         //string sql = "select * from Person where PID>‘" + (Convert.ToInt32(lbPage.Text) - 1) * countPage + "‘ and PID<=‘" + Convert.ToInt32(lbPage.Text) * countPage + "‘ order by PID ASC";
182 
183         string sql = @"select * from
184                         (
185                         select ROW_NUMBER() over(order by PID) as rownum, PID, PName, PAge, PSex, PJob from Person
186                         ) as a
187                         where a.rownum > ‘" + (Convert.ToInt32(lbPage.Text) - 1) * countPage + "‘ and a.rownum <=‘" + Convert.ToInt32(lbPage.Text) * countPage + "‘ order by a.rownum ASC";
188 
189         sqlCmd = new SqlCommand(sql, sqlConn);
190         SqlDataAdapter sAdapter = new SqlDataAdapter(sqlCmd);
191         DataSet ds = new DataSet();
192         sAdapter.Fill(ds, "Result");
193         sqlConn.Close();
194         Repeater1.DataSource = ds.Tables["Result"].DefaultView;
195         Repeater1.DataBind();
196     }
197     protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
198     {
199         if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
200         {
201             int row = e.Item.ItemIndex;
202 
203             HiddenField hfId = e.Item.FindControl("hfId") as HiddenField;
204 
205             int Id = int.Parse(hfId.Value);
206 
207             string cmdName = e.CommandName.Trim();
208 
209             switch (cmdName)
210             {
211                 case "EditData":
212                     {
213                         Response.Redirect("Edit.aspx?PID=" + Id.ToString());
214                     }break;
215                 case "DelData":
216                     {
217                         sqlConn.Open();
218 
219                         SqlParameter[] para = new SqlParameter[]
220                         {
221                             new SqlParameter("@PID", Id)
222                         };
223 
224                         string txt = "DELETE  FROM Person WHERE PID = @PID";
225 
226                         SqlCommand cmd = new SqlCommand(txt, sqlConn);
227 
228                         cmd.Parameters.AddRange(para);
229 
230                         int res = cmd.ExecuteNonQuery();
231 
232                         if (res > 0)
233                         {
234                             Response.Write("<script language=‘javascript‘>alert(‘删除成功!‘);</script>");
235                         }
236                         else
237                         {
238                             Response.Write("<script language=‘javascript‘>alert(‘无法删除!‘);</script>");
239                         }
240 
241                         Show();
242 
243                         State();
244 
245                         sqlConn.Close();
246                     }break;
247                 default:
248                     break;
249             }
250         }
251     }
252 }
View Code

 

 

紧接着是Edit.aspx页面的前台代码:

技术分享
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Edit.aspx.cs" Inherits="Edit" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12     <div>
13         <asp:Label ID="Label1" runat="server" Text="姓名"></asp:Label>
14         <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
15 
16         <br /><br />
17 
18         <asp:Label ID="Label2" runat="server" Text="年龄"></asp:Label>
19         <asp:TextBox ID="txt_age" runat="server"></asp:TextBox>
20 
21         <br /><br />
22 
23         <asp:Label ID="Label3" runat="server" Text="性别"></asp:Label>
24         <asp:TextBox ID="txt_sex" runat="server"></asp:TextBox>
25 
26         <br /><br />
27 
28         <asp:Label ID="Label4" runat="server" Text="工作"></asp:Label>
29         <asp:TextBox ID="txt_job" runat="server"></asp:TextBox>
30 
31         <br /><br />
32 
33         <asp:Button ID="Button1" runat="server" Text="确认修改" OnClick ="Button1_Click"/>
34         <asp:Button ID="Button2" runat="server" Text="重置" />
35     </div>
36     </form>
37 </body>
38 </html>
View Code

 

和Edit.aspx的后台代码:

技术分享
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Web.UI;
  6 using System.Web.UI.WebControls;
  7 
  8 using System.Data;
  9 using System.Data.SqlClient;
 10 using System.Configuration;
 11 
 12 public partial class Edit : System.Web.UI.Page
 13 {
 14     string connectionString = ConfigurationManager.ConnectionStrings["LinqTestConnectionString"].ConnectionString;
 15 
 16     string SPID = string.Empty;
 17 
 18     protected void Page_Load(object sender, EventArgs e)
 19     {
 20         if (!IsPostBack)
 21         {
 22             databind();
 23         }
 24     }
 25 
 26     public void databind()
 27     {
 28         if (Request.QueryString["PID"].ToString() != null)
 29         {
 30             SPID = Request.QueryString["PID"].ToString();
 31         }
 32         else
 33         {
 34             Response.Write("<script language=‘javascript‘>alert(‘没有值!‘);</script>");
 35         }
 36 
 37         SqlConnection connection = new SqlConnection(connectionString);
 38 
 39         SqlParameter[] para = new SqlParameter[]
 40         {
 41             new SqlParameter("@SPID", SPID)
 42         };
 43 
 44         string sql = "SELECT PName, PSex, PAge, PJob FROM Person WHERE PID = @SPID";
 45 
 46         connection.Open();
 47 
 48         SqlDataAdapter adp = new SqlDataAdapter(sql, connection);
 49 
 50         adp.SelectCommand.Parameters.AddRange(para);//又是一个新的知识点,使用SqlDataAdapter时,如何还能使用参数。
 51 
 52         DataTable dt = new DataTable();
 53 
 54         adp.Fill(dt);
 55 
 56         this.txt_name.Text = dt.Rows[0][0].ToString();
 57         this.txt_name.Focus();
 58         this.txt_age.Text = dt.Rows[0][2].ToString();
 59         this.txt_sex.Text = dt.Rows[0][1].ToString();
 60         this.txt_job.Text = dt.Rows[0][3].ToString();
 61 
 62         connection.Close();
 63     }
 64 
 65     protected void Button1_Click(object sender, EventArgs e)
 66     {
 67         SPID = Request.QueryString["PID"].ToString();
 68 
 69         SqlConnection connection_2 = new SqlConnection(connectionString);
 70 
 71         connection_2.Open();
 72 
 73         SqlParameter[] para_2 = new SqlParameter[]
 74         {
 75             new SqlParameter ("@SPID", SPID),
 76             new SqlParameter("@pname", this.txt_name.Text),
 77             new SqlParameter("@page", this.txt_age.Text),
 78             new SqlParameter("@psex", this.txt_sex.Text),
 79             new SqlParameter("@pjob", this.txt_job.Text)
 80         };
 81 
 82         string sql_2 = "UPDATE Person SET PName = @pname, PAge = @page, PSex = @psex, PJob = @pjob WHERE PID = @SPID";
 83 
 84         SqlCommand cmd_2 = new SqlCommand(sql_2, connection_2);
 85 
 86         cmd_2.Parameters.AddRange(para_2);
 87 
 88         int res = cmd_2.ExecuteNonQuery();
 89 
 90         if (res > 0)
 91         {
 92             Response.Write("<script language=‘javascript‘>alert(‘修改成功!‘);</script>");
 93             Response.Write("<script language=‘javascript‘>window.location.href=‘Default.aspx‘;</script>");
 94         }
 95         else
 96         {
 97             Response.Write("<script language=‘javascript‘>alert(‘修改失败!‘);</script>");
 98         }
 99     }
100     /// <summary>
101     /// 清空文本框
102     /// </summary>
103     /// <param name="sender"></param>
104     /// <param name="e"></param>
105     protected void Button2_Click(object sender, EventArgs e)
106     {
107         this.txt_name.Text = "";
108         this.txt_age.Text = "";
109         this.txt_sex.Text = "";
110         this.txt_job.Text = "";
111     }
112 }
View Code

 

文件夹目录:

技术分享

 

 

运行效果,在开头已经给出。

 

Repeater控件-实现分页(升级版)

标签:

原文地址:http://www.cnblogs.com/KTblog/p/4280053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!