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

 

*测试的时候,一定要从登陆界面启动,因为要从登陆页面传值给显示信息的页面。否则,没有值传入,那么显示页面也就没信息可显示。

 

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

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

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

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

 

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

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

以上就是,新增的内容。

 

下面是代码:

 

总计使用了两个页面,一个是显示数据的页面(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

通用代码:

 1 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Paging.ascx.cs" Inherits="UserControl.Paging" %>

 2 <div id ="DataShow">

 3         <asp:Repeater ID="Repeater1" runat="server">

 4         <HeaderTemplate>

 5                 <table>

 6                 <tr><td colspan="2" style="text-align:center; background-color:#76a0ae; color:#ffffff; height:20px">标题(待修改)

 7 

 8 </td></tr>

 9                 <tr style="color:#1e486e">

10                 <td>显示字段(待修改)</td>

11                 <td>显示字段(待修改</td>

12                 <td colspan="2">操作</td>

13                 </tr>

14         </HeaderTemplate>

15         <ItemTemplate>

16                 <tr>

17                     <td>

18                         <%#Eval("绑定字段(待修改)")%>

19                     </td>

20 

21                     <td>

22                         <%#Eval("绑定字段(待修改)") %>

23                     </td>

24 

25                     <td>

26                         <asp:LinkButton ID="lnkEdit" CommandArgument ='<%#Eval("绑定字段(待修改)")%>' runat="server" OnClick 

27 

28 ="lnkEdit_Click">修改</asp:LinkButton>

29                     </td>

30 

31                     <td>

32                         <asp:LinkButton ID="lnkDelete" CommandArgument ='<%#Eval("绑定字段(待修改)")%>' runat="server" OnClick 

33 

34 ="lnkDelete_Click">删除</asp:LinkButton>

35                     </td>

36 

37                 </tr>

38         </ItemTemplate>

39         <FooterTemplate>

40                         <tr id="NoData" runat="server" Visible="<%#Repeater1.Items.Count==0 %>"  >

41                             <td colspan="2">

42                                 <div>

43                                     <p style ="text-align:center">没有相关的数据!</p>

44                                 </div>

45                             </td>

46                       </tr>

47                 </table>

48         </FooterTemplate>

49         </asp:Repeater>

50     </div>

51 <div id="DataOperation">

52     <asp:Label ID="lbNow" runat="server" Text="当前页"></asp:Label>

53     <asp:Label ID="lbPage" runat="server" Text="1"></asp:Label>

54     <asp:Label ID="lbAll" runat="server" Text="总页数"></asp:Label>

55     <asp:Label ID="lbCount" runat="server"></asp:Label>

56     <asp:LinkButton ID="lbtnFirst" runat="server">首页</asp:LinkButton>

57     <asp:LinkButton ID="lbtnUp" runat="server">上一页</asp:LinkButton>

58     <asp:LinkButton ID="lbtnDown" runat="server">下一页</asp:LinkButton>

59     <asp:LinkButton ID="lbtnLast" runat="server">尾页</asp:LinkButton>

60     <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>

61     <asp:LinkButton ID="lbtnGo" runat="server">跳转</asp:LinkButton>

62 </div>
通用代码

 

 

 

 

然后是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

优化代码:

  1 using System;

  2 using System.Configuration;

  3 using System.Data;

  4 using System.Data.SqlClient;

  5 

  6 namespace UserControl

  7 {

  8     public partial class Paging : System.Web.UI.UserControl

  9     {

 10         SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ToString());

 11 

 12         public SqlCommand sqlCmd = null;

 13 

 14         /// <summary>

 15         /// 按照哪一个字段进行排序(在进行查询记录时)

 16         /// </summary>

 17         public string OderbyField = "";

 18 

 19         /// <summary>

 20         /// 查询字段(中间以逗号隔开,开头不加逗号,末尾不加逗号)

 21         /// </summary>

 22         public string QueryField = "";

 23 

 24         /// <summary>

 25         /// 表名称

 26         /// </summary>

 27         public string TableName = "";

 28 

 29         /// <summary>

 30         /// 每一页显示数据量

 31         /// </summary>

 32         public int CountPage = ;

 33 

 34         protected void Page_Load(object sender, EventArgs e)

 35         {

 36             DropListBind();//为DropDownList赋值

 37             Show();//初始化显示第一页,默认当前为第一页

 38             State();//初始化导航按钮的使用状态

 39         }

 40 

 41         /// <summary>

 42         /// 修改信息

 43         /// </summary>

 44         /// <param name="sender"></param>

 45         /// <param name="e"></param>

 46         protected void lnkEdit_Click(object sender, EventArgs e)

 47         {

 48             //删除还有没写

 49         }

 50 

 51         /// <summary>

 52         /// 删除信息

 53         /// </summary>

 54         /// <param name="sender"></param>

 55         /// <param name="e"></param>

 56         protected void lnkDelete_Click(object sender, EventArgs e)

 57         {

 58             //修改还有没写

 59         }

 60 

 61         /// <summary>

 62         /// 首页

 63         /// </summary>

 64         /// <param name="sender"></param>

 65         /// <param name="e"></param>

 66         protected void lbtnFirst_Click(object sender, EventArgs e)

 67         {

 68             lbPage.Text = "1";

 69             Show();

 70             State();

 71         }

 72 

 73         /// <summary>

 74         /// 上一页

 75         /// </summary>

 76         /// <param name="sender"></param>

 77         /// <param name="e"></param>

 78         protected void lbtnUp_Click(object sender, EventArgs e)

 79         {

 80             lbPage.Text = (Convert.ToInt32(lbPage.Text) - 1).ToString();

 81             Show();

 82             State();

 83         }

 84 

 85         /// <summary>

 86         /// 下一页

 87         /// </summary>

 88         /// <param name="sender"></param>

 89         /// <param name="e"></param>

 90         protected void lbtnDown_Click(object sender, EventArgs e)

 91         {

 92             lbPage.Text = (Convert.ToInt32(lbPage.Text) + 1).ToString();

 93             Show();

 94             State();

 95         }

 96 

 97         /// <summary>

 98         /// 尾页

 99         /// </summary>

100         /// <param name="sender"></param>

101         /// <param name="e"></param>

102         protected void lbtnLast_Click(object sender, EventArgs e)

103         {

104             lbPage.Text = lbCount.Text;

105             Show();

106             State();

107         }

108 

109         //绑定DropDwonList控件

110         public void DropListBind()

111         {

112             sqlConn.Open();

113 

114             sqlCmd = new SqlCommand("select count(*) from " + TableName + "", sqlConn);//获取数据库中信息的总条数

115 

116             int page = Convert.ToInt32(sqlCmd.ExecuteScalar());

117 

118             //如果当前页1,且数据库中的数据行小于每一页要显示的数据行,则Label控件全部设置为不可用。

119             if (Convert.ToInt32(lbPage.Text) == 1 && (page - CountPage) < 0)

120             {

121                 lbtnFirst.Enabled = false;

122                 lbtnUp.Enabled = false;

123                 lbtnLast.Enabled = false;

124                 lbtnDown.Enabled = false;

125             }

126 

127             //每页显示countPage条,算出总页数,并为DropDownList赋值

128 

129             //使用ceiling(天花板函数)--MSDN示例

130             /*

131                 *        double[] values = {7.03, 7.64, 0.12, -0.12, -7.1, -7.6};

132                 *        Console.WriteLine("  Value          Ceiling          Floor\n");

133                 *        foreach (double value in values)

134                 *          {

135                 *              Console.WriteLine("{0,7} {1,16} {2,14}", value, Math.Ceiling(value), Math.Floor(value));

136                 *          }

137                 *        The example displays the following output to the console:

138                 *                Value          Ceiling          Floor

139                 *                 7.03                8              7

140                 *                 7.64                8              7

141                 *                 0.12                1              0

142                 *                -0.12                0             -1

143                 *                -7.1                -7             -8

144                 *                -7.6                -7             -8

145                 */

146             //为什么Page要乘以1.0,因为如果不乘的话,就会默认转换成整型,得不到小数,就无法使用ceiling方法了。

147             this.lbCount.Text = (Math.Ceiling(((page * 1.0 / CountPage)))).ToString();

148 

149             int[] num = new int[Convert.ToInt32(lbCount.Text)];

150 

151             for (int i = 1; i <= Convert.ToInt32(lbCount.Text); i++)//如果使用 i= 0,那么在前台显示的时候第一个值是0.

152             {

153                 num[i - 1] = i;

154             }

155 

156             sqlConn.Close();

157 

158             DropDownList1.DataSource = num;

159 

160             DropDownList1.DataBind();

161         }

162 

163         /// <summary>

164         /// 状态设置

165         /// </summary>

166         public void State()

167         {

168             if (lbPage.Text == "1")//如果当前页为第一页,则前一页和首页按钮禁用

169             {

170                 lbtnFirst.Enabled = false;

171                 lbtnUp.Enabled = false;

172                 lbtnLast.Enabled = true;

173                 lbtnDown.Enabled = true;

174             }

175 

176             if (lbPage.Text == lbCount.Text)//如果当前页为最后一页,则后一页和尾页按钮禁用

177             {

178                 lbtnFirst.Enabled = true;

179                 lbtnUp.Enabled = true;

180                 lbtnLast.Enabled = false;

181                 lbtnDown.Enabled = false;

182             }

183             if (Convert.ToInt32(lbPage.Text) > 1 && Convert.ToInt32(lbPage.Text) < Convert.ToInt32(lbCount.Text))//如果当前也在首页和

184 

185 尾页之间则四个按钮均可用

186             {

187                 lbtnFirst.Enabled = true;

188                 lbtnUp.Enabled = true;

189                 lbtnLast.Enabled = true;

190                 lbtnDown.Enabled = true;

191             }

192         }

193 

194         /// <summary>

195         /// 显示数据,绑定数据

196         /// </summary>

197         public void Show()

198         {

199             //从数据库中筛选信息,仅加载当前请求的那一页的信息,效率会相对比较高,并非全部加载

200 

201 

202             //string sql = "select * from Person where PID>'" + (Convert.ToInt32(lbPage.Text) - 1) * countPage + "' and PID<='" + 

203 

204 Convert.ToInt32(lbPage.Text) * countPage + "' order by PID ASC";

205 

206             string sql = @"select * from

207                         (

208                         select ROW_NUMBER() over(order by " + OderbyField + ") as rownum,  " + QueryField + "  from " + TableName + 

209 

210 ") as a where a.rownum > '" + (Convert.ToInt32(lbPage.Text) - 1) * countPage + "' and a.rownum <='" + Convert.ToInt32(lbPage.Text) * 

211 

212 countPage + "' order by a.rownum ASC";

213 

214             sqlCmd = new SqlCommand(sql, sqlConn);

215             SqlDataAdapter sAdapter = new SqlDataAdapter(sqlCmd);

216             DataSet ds = new DataSet();

217             sAdapter.Fill(ds, "Result");

218             sqlConn.Close();

219             Repeater1.DataSource = ds.Tables["Result"].DefaultView;

220             Repeater1.DataBind();

221         }

222 

223         /// <summary>

224         /// 跳转

225         /// </summary>

226         /// <param name="sender"></param>

227         /// <param name="e"></param>

228         protected void lbtnGo_Click(object sender, EventArgs e)

229         {

230             lbPage.Text = this.DropDownList1.SelectedValue;

231             Show();

232             State();

233         }

234     }

235 }

236 

237 优化代码
通用代码

 

 

 

 

 

 

 

紧接着是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控件-实现分页(升级版)

 

 

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

 

你可能感兴趣的:(分页)