用存储过程实现Repeater的分页

建立一个表:post,字段:ID(int,主键),a1,a2,a3,a4,a5,a6

 

建立一个存储过程,代码如下:

  1. ALTER proc [dbo].[GetDataSet]
  2. @TableList Varchar(200)='*',--搜索表的字段,比如:’id,datatime,job‘,用逗号隔开
  3. @TableName Varchar(100), --搜索的表名
  4. @SelectWhere Varchar(500),--搜索条件,这里不用写where,比如:job=’teacher‘and class='2'
  5. @SelectOrderId Varchar(20),--表主键字段名。比如:id
  6. @SelectOrder Varchar(200)='', --排序,可以使用多字段排序但主键字段必需在最前面.也可以不写,比如:order by class asc
  7. @intPageNo int=1, --页号
  8. @intPageSize int=10 ,--每页显示数
  9. @RecordCount int OUTPUT   --总记录数(存储过程输出参数)
  10. as  
  11. declare @TmpSelect    NVarchar(600)  
  12. declare @Tmp     NVarchar(600)  
  13. set nocount on--关闭计数
  14. set @TmpSelect = 'select @RecordCount = count(*) from '+@TableName+' '+@SelectWhere
  15. execute sp_executesql 
  16. @TmpSelect, --执行上面的sql语句
  17. N'@RecordCount int OUTPUT' , --执行输出数据的sql语句,output出总记录数
  18. @RecordCount   OUTPUT
  19.    --if (@RecordCount = 0) --如果没有贴子,则返回零
  20.    --return 0
  21.    
  22. /*判断页数是否正确*/
  23.    if (@intPageNo - 1) * @intPageSize > @RecordCount --页号大于总页数,返回错误
  24.     return (-1)
  25. set nocount off--打开计数
  26. if @SelectWhere != '' 
  27. begin
  28. set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' '+@SelectWhere+' and '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectWhere +' '+@SelectOrder+') '+@SelectOrder
  29. end
  30. else
  31. begin
  32. set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectOrder+') '+@SelectOrder
  33. end
  34. execute sp_executesql @TmpSelect
  35. return(@@rowcount)

新建一个post.aspx文件,写入以下代码:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="post.aspx.cs" Inherits="post" %>
  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>无标题页</title>
  6.     <script type="text/javascript"> 
  7.         function CheckAllGridCheckBox(formName,obj, name)
  8.         {
  9.             var elements = document.forms[formName].elements;
  10.             for (var i=0; i<elements.length; i++)
  11.             {
  12.                 if (elements[i].type == 'checkbox')
  13.                 {
  14.                     if (elements[i].name.indexOf(name) > -1)
  15.                     {
  16.                         elements[i].checked = obj.checked;
  17.                     }
  18.                 }
  19.             }
  20.         }
  21.         
  22.         function showsubmenu(sid)
  23.         {
  24.             whichEl = eval("info" + sid);
  25.             if (whichEl.style.display == "none")
  26.             {
  27.                 eval("info" + sid + ".style.display=/"/";");
  28.             }
  29.             else
  30.             {
  31.                 eval("info" + sid + ".style.display=/"none/";");
  32.             }
  33.         }
  34.     </script>
  35. </head>
  36. <body>
  37.     <form id="form1" runat="server">
  38.     <asp:Repeater ID="PostList" runat="server">
  39.                 <HeaderTemplate>
  40.                 <table width="100%" border="0" cellpadding="0" cellspacing="0">
  41.                     <tr>
  42.                         <td style="text-align:center; background:#F1D4D8; height: 30px; width:5%; border-top:#DAC6C7 1px solid;"> </td>
  43.                         <td style="text-align:center; background:#F1D4D8; height: 30px; width:5%; border-top:#DAC6C7 1px solid;"> </td>
  44.                         <td style="text-align:center; background:#F1D4D8; height: 30px; width:10%; border-top:#DAC6C7 1px solid;">标题</td>
  45.                         <td style="text-align:center; background:#F1D4D8; height: 30px; width:45%; border-top:#DAC6C7 1px solid;">标题</td>
  46.                         <td style="text-align:center; background:#F1D4D8; height: 30px; width:10%; border-top:#DAC6C7 1px solid;">标题</td>
  47.                         <td style="text-align:center; background:#F1D4D8; height: 30px; width:10%; border-top:#DAC6C7 1px solid;">标题</td>
  48.                         <td style="text-align:center; background:#F1D4D8; height: 30px; width:10%; border-top:#DAC6C7 1px solid;">标题</td>
  49.                         <td style="text-align:center; background:#F1D4D8; height: 30px; width:5%; border-top:#DAC6C7 1px solid;">标题</td>
  50.                     </tr>
  51.                 </HeaderTemplate>
  52.                 <ItemTemplate>
  53.                     <tr>
  54.                        <td height="30" align="center"><input id="PostID" name="PostID" runat="server" type="checkbox" value='<%# Eval("ID") %>' /></td>
  55.                        <td align="center"><%# Eval("ID") %></td>
  56.                        <td align="center"><%# Eval("a1") %></td>
  57.                        <td align="center"><%# Eval("a2") %></td>
  58.                        <td align="center"><%# Eval("a3") %></td>
  59.                        <td align="center"><%# Eval("a4") %></td>
  60.                        <td align="center"><%# Eval("a5") %></td>
  61.                        <td align="center"><%# Eval("a6") %></td>
  62.                     </tr>
  63.                 </ItemTemplate>
  64.                 <FooterTemplate>
  65.                 </table>
  66.                 </FooterTemplate>
  67.                 </asp:Repeater>
  68.                 <div style="width:100%; text-align:left;">
  69.                     <input type=checkbox id="allcheck" onclick="CheckAllGridCheckBox('form1',this,'PostID')" />全选
  70.                 </div>
  71.                 <div style="text-align:center;">
  72.                    <asp:HyperLink ID="hylfirst" runat="server">首页</asp:HyperLink>
  73.                    <asp:HyperLink ID="hylprev" runat="server">上一页</asp:HyperLink>
  74.                    <asp:HyperLink ID="hylnext" runat="server">下一页</asp:HyperLink>
  75.                    <asp:HyperLink ID="hylend" runat="server">尾页</asp:HyperLink>  
  76.                     第<asp:Label ID="lbRow" runat="server" Text="Label"></asp:Label>页,共<asp:Label ID="lbpage" runat="server" Text="Label"></asp:Label>页,共<asp:Label
  77.                      ID="lbRecord" runat="server" Text="Label"></asp:Label>条记录  转到<asp:TextBox ID="txtlink"
  78.                         runat="server" Width="29px"></asp:TextBox>
  79.                    页 <asp:LinkButton ID="link" runat="server" OnClick="link_Click" TabIndex="1">转到</asp:LinkButton>
  80.                 </div>
  81.     </form>
  82. </body>
  83. </html>

post.aspx.cs文件的代码:

  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12. public partial class post : System.Web.UI.Page
  13. {
  14.     SqlHelper SH = new SqlHelper();
  15.     //这里定义存储过程分页中需要的参数
  16.     //TableList:要读取的字段名
  17.     //TableName:要读取的表名
  18.     //SelectWhere:读取数据的条件,如果没有条件,默认必须为:where 1=1
  19.     //SelectOrderId:需要索引的字段
  20.     //SelectOrder:需要排序的条件
  21.     //intPageNo:默认第几页
  22.     //intPageSize:每页显示多少条
  23.     string TableList = "ID,a1,a2,a3,a4,a5,a6";
  24.     string TableName = "post";
  25.     string SelectWhere = "where 1=1";
  26.     string SelectOrderId = "id";
  27.     string SelectOrder = "order by id desc";
  28.     int intPageNo = 1;
  29.     int intPageSize = 15;
  30.     
  31.     protected void Page_Load(object sender, EventArgs e)
  32.     {
  33.         if (!Page.IsPostBack)
  34.         {
  35.             //调用绑定数据的过程,并且传递参数
  36.             bind(TableList, TableName, SelectWhere, SelectOrderId, SelectOrder, intPageNo, intPageSize);
  37.         }
  38.     }
  39.     #region 绑定数据,带分页
  40.     protected void bind(string TableList, string TableName, string SelectWhere, string SelectOrderId, string SelectOrder, int intPageNo, int intPageSize)
  41.     {
  42.         int sumPage;
  43.         int pageNo = 1;
  44.         int pageSize = 15;
  45.         if (Request.QueryString["Page"] == null)
  46.         {
  47.             pageNo = 1;
  48.         }
  49.         else
  50.         {
  51.             pageNo = Int32.Parse(Request.QueryString["Page"]);
  52.         }
  53.         int RecordCount;
  54.         DataSet ds = SH.DataSelect(TableList, TableName, SelectWhere, SelectOrderId, SelectOrder, pageNo, intPageSize, out RecordCount);
  55.         if (ds.Tables.Count == 0)
  56.         {
  57.             Response.Redirect("post.aspx");
  58.         }
  59.         PostList.DataSource = ds;
  60.         PostList.DataBind();
  61.         PostList.Dispose();
  62.         lbRecord.Text = RecordCount.ToString();
  63.         lbRow.Text = pageNo.ToString();
  64.         sumPage = (Int32)RecordCount / pageSize;
  65.         if (RecordCount % pageSize > 0)
  66.         {
  67.             sumPage = sumPage + 1;
  68.         }
  69.         lbpage.Text = sumPage.ToString();
  70.         if (pageNo > 1)
  71.         {
  72.             hylfirst.NavigateUrl = "?Page=1";
  73.             hylprev.NavigateUrl = string.Concat("?Page=""", pageNo - 1);
  74.         }
  75.         else
  76.         {
  77.             hylprev.NavigateUrl = "";
  78.             hylfirst.NavigateUrl = "";
  79.             hylfirst.Visible = false;
  80.             hylprev.Visible = false;
  81.         }
  82.         if (pageNo < sumPage)
  83.         {
  84.             hylend.NavigateUrl = string.Concat("?Page=""", sumPage);
  85.             hylnext.NavigateUrl = string.Concat("?Page=""", pageNo + 1);
  86.         }
  87.         else
  88.         {
  89.             hylnext.NavigateUrl = "";
  90.             hylend.NavigateUrl = "";
  91.             hylend.Visible = false;
  92.             hylnext.Visible = false;
  93.         }
  94.     }
  95.     #endregion
  96.     #region 点击转到第几页按钮
  97.     protected void link_Click(object sender, EventArgs e)
  98.     {
  99.         int page = Convert.ToInt32(txtlink.Text);
  100.         Response.Redirect("post.aspx?Page=" + page + "");
  101.     }
  102.     #endregion
  103.     #region 删除选定的数据
  104.     protected void btnDelete_Click(object sender, EventArgs e)
  105.     {
  106.         string idList = GetIdList();
  107.         if (idList.Length > 0)
  108.         {
  109.             string sql = "delete from post where ID in (" + idList + ")";
  110.             SH.ExecNon(sql);
  111.             //调用绑定数据的过程,并且传递参数
  112.             bind(TableList, TableName, SelectWhere, SelectOrderId, SelectOrder, intPageNo, intPageSize);
  113.         }
  114.     }
  115.     #endregion
  116.     #region 获取选中新闻的ID列表
  117.     private string GetIdList()
  118.     {
  119.         string idList = "";
  120.         HtmlInputCheckBox chk;
  121.         foreach (RepeaterItem item in PostList.Items)
  122.         {
  123.             chk = item.FindControl("PostID"as HtmlInputCheckBox;
  124.             if (chk != null && chk.Checked)
  125.             {
  126.                 //字符类型
  127.                 idList += ((HtmlInputCheckBox)(item.FindControl("PostID"))).Value + ",";
  128.             }
  129.         }
  130.         idList = idList.TrimEnd(',');
  131.         return idList;
  132.     }
  133.     #endregion
  134. }

写一个数据库通用读取类(其中包括调用存储过程):

  1. using System;
  2. using System.Text;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Configuration;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12. /// 
  13. /// ASP.NET SQLSERVER数据库通用操作类(封装了一些常用数据)
  14. /// 
  15. public class SqlHelper
  16. {
  17.     public SqlHelper()
  18.     {
  19.         //
  20.         // 此处: 添加构造函数
  21.         //
  22.     }
  23.     public static string ConnectionString = ConfigurationManager.ConnectionStrings["zzyjsConn"].ConnectionString;
  24. //这里的zzyjsconn是web.config中的数据库连接字符串名称
  25.     //执行SQL语句,无返回值
  26.     #region ExecNon
  27.     public void ExecNon(string sql)
  28.     {
  29.         using (SqlConnection conn = new SqlConnection(ConnectionString))
  30.         {
  31.             using (SqlCommand cmd = new SqlCommand(sql, conn))
  32.             {
  33.                 cmd.Connection = conn;
  34.                 conn.Open();
  35.                 try
  36.                 {
  37.                     cmd.ExecuteNonQuery();
  38.                 }
  39.                 catch (Exception ex)
  40.                 {
  41.                     throw ex;
  42.                 }
  43.                 finally
  44.                 {
  45.                     conn.Close();
  46.                     conn.Dispose();
  47.                 }
  48.             }
  49.         }
  50.     }
  51.     #endregion
  52.     ///利用存储过程分页
  53.     #region DataSelect
  54.     ///参数说明:
  55.     ///TableList 要取得的字段列表,全部为*
  56.     ///TableName 要查询的表名
  57.     ///SelectWhere 查询条件,不可为空
  58.     ///SelectOrderID 排序字段
  59.     ///SelectOrder 排序方式
  60.     ///intPageNo 当前页码
  61.     ///intPageSize 每页显示的记录数
  62.     ///RecordCount 返回值,查询出来的总记录数,传递参数时无需初始化
  63.     public DataSet DataSelect(string TableList, string TableName, string SelectWhere, string SelectOrderId, string SelectOrder, int intPageNo, int intPageSize, out int RecordCount)
  64.     {
  65.         SqlConnection conn = new SqlConnection(ConnectionString);
  66.         SqlDataAdapter da = new SqlDataAdapter();
  67.         da.SelectCommand = new SqlCommand();
  68.         da.SelectCommand.Connection = conn;
  69.         da.SelectCommand.CommandText = "getdataset";
  70.         da.SelectCommand.CommandType = CommandType.StoredProcedure;
  71.         da.SelectCommand.Parameters.Add("@TableList", SqlDbType.VarChar, 200).Value = TableList;
  72.         da.SelectCommand.Parameters.Add("@TableName", SqlDbType.VarChar, 100).Value = TableName;
  73.         da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = SelectWhere;
  74.         da.SelectCommand.Parameters.Add("@SelectOrderId", SqlDbType.VarChar, 20).Value = SelectOrderId;
  75.         da.SelectCommand.Parameters.Add("@SelectOrder", SqlDbType.VarChar, 200).Value = SelectOrder;
  76.         da.SelectCommand.Parameters.Add("@intPageNo", SqlDbType.Int).Value = intPageNo;
  77.         da.SelectCommand.Parameters.Add("@intPageSize", SqlDbType.Int).Value = intPageSize;
  78.         da.SelectCommand.Parameters.Add("@RecordCount", SqlDbType.Int).Direction = ParameterDirection.Output;
  79.         DataSet ds = new DataSet();
  80.         da.Fill(ds);
  81.         RecordCount = (Int32)da.SelectCommand.Parameters["@RecordCount"].Value; //求出总记录数,该值是output出来的值
  82.         return ds;
  83.         conn.Close();
  84.     }
  85.     #endregion
  86. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值