C#分页插件 Webdiyer

Repeater没有自带的分页功能,搜罗了一下发现AspNetPager.dll这个插件比较好用拿出来分享一下,老鸟勿喷

先来一张效果图

该插件的好处在这里就不多说了,直接上代码

(1)首先需要把AspNetPager.dll引用到项目中来

(2)Aspx代码

ASPX代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo.aspx.cs" Inherits="WebDiyer.Demo" %>

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>分页测试</title>
     <link href="Style/Pager.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        function CgColor(type, obj) {
            if (type == "click") {
                for (var i = 1; i < document.getElementById("list").rows.length; i++) {
                    document.getElementById("list").rows[i].style.backgroundColor = "";
                    document.getElementById("list").rows[i].tag = false;
                }
                obj.style.backgroundColor = "#E1E9FD";
                obj.tag = true;
            }
            else if (type == "over") {
                if (!obj.tag) {
                    obj.style.backgroundColor = "#EEF2FB";
                }
            }
            else if (type == "out") {
                if (!obj.tag) {
                    obj.style.backgroundColor = "";
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <div style="border: 1px solid #CCC; padding: 5px; margin-top: 5px; margin-bottom: 5px;">
                    Order ID:<asp:DropDownList ID="DropDownList1" runat="server">
                        <asp:ListItem>&gt;=</asp:ListItem>
                        <asp:ListItem>&lt;=</asp:ListItem>
                        <asp:ListItem>=</asp:ListItem>
                    </asp:DropDownList>
                    <asp:TextBox ID="tb_orderid" runat="server" Width="90px"></asp:TextBox>
                    <asp:Button ID="btn_search" runat="server" OnClick="btn_search_Click" Text="Search" />
                    <asp:Button ID="btn_all" runat="server" OnClick="btn_all_Click" Text="Show All" Enabled="false" />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tb_orderid"
                        Display="Dynamic" ErrorMessage="RequiredFieldValidator">必需</asp:RequiredFieldValidator>&nbsp;
                    <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="tb_orderid"
                        Display="Dynamic" ErrorMessage="CompareValidator" Operator="DataTypeCheck" SetFocusOnError="True"
                        Type="Integer">必须是整数</asp:CompareValidator>
                </div>
                <div style="height:450px">
                <asp:Repeater ID="rpRuleList" runat="server">
                    <HeaderTemplate>
                        <table width="100%" border="1" cellspacing="0" cellpadding="4" style="border-collapse: collapse"
                            id="list">
                            <tr>
                                <th style="width: 5%">
                                    序号
                                </th>
                                <th style="width: 10%">
                                    订单编号
                                </th>
                                <th style="width: 15%">
                                    订单日期
                                </th>
                                <th style="width: 30%">
                                    公司名称
                                </th>
                                <th style="width: 20%">
                                    客户编号
                                </th>
                                <th style="width: 20%">
                                    雇员姓名
                                </th>
                            </tr>

                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr οnmοuseοver="CgColor('over',this)" οnmοuseοut="CgColor('out',this)" οnclick="CgColor('click',this)">
                            <td>
                                <%#Eval("rowNumber")%>
                            </td>
                            <td>
                                <%#Eval("orderid")%>
                            </td>
                            <td>
                                <%#Eval("orderdate","{0:d}")%>
                            </td>
                            <td>
                                <%#Eval("companyname")%>
                            </td>
                            <td>
                                <%#Eval("customerid")%>
                            </td>
                            <td>
                                <%#Eval("employeename")%>
                            </td>
                        </tr>
                        
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                    
                </asp:Repeater>
                </div>
                
                <webdiyer:AspNetPager ID="AspNetPager1" runat="server" PageSize="15" 
                    OnPageChanged="AspNetPager1_PageChanged"  CssClass="cssPager"
                    FirstPageText="首页" LastPageText="尾页" PrevPageText="上页"
                    NextPageText="下页" HorizontalAlign="left" >
                </webdiyer:AspNetPager>

            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

 

(3)CS代码

CS代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace WebDiyer
{
    public partial class Demo : System.Web.UI.Page
    {
        #region
        string count;//用于记录记录集条数
        const string vsKey = " "; //ViewState key
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                searchOrders(string.Empty);
            }
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            btn_all.Enabled = true;
            string s = "  orderid " + DropDownList1.SelectedValue + tb_orderid.Text;
            ViewState[vsKey] = s;
            searchOrders(s);
        }

        void searchOrders(string sWhere)
        {
            string pageIndex = (AspNetPager1.CurrentPageIndex ).ToString();//当前页码
            DataTable dt = Common.DB.GetPage("V_Orders", "orderid,orderdate,customerid,CompanyName,EmployeeName ", pageIndex, sWhere, "orderid desc", "15", out count);
            AspNetPager1.RecordCount =Convert.ToInt32(count);
            rpRuleList.DataSource = dt;
            rpRuleList.DataBind();
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            searchOrders((string)ViewState[vsKey]);
        }
        protected void btn_all_Click(object sender, EventArgs e)
        {
            ViewState[vsKey] = null;
            btn_all.Enabled = false;
            AspNetPager1.CurrentPageIndex = 1;
            searchOrders(null);
        }
        #endregion
    }
    

}

 

(4)涉及到的存储过程

涉及到的分页存储过程
ALTER  procedure [dbo].[GetPage]
    @tblName      varchar(1000),       -- 表名(必要参数)
    @Fields      varchar(1000)='*',      --要返回的列,要保证没有名称的列有别名
    @PageSize     int = 25,           -- 页尺寸
    @PageIndex    int = 1,            -- 页码
    @OrderType    varchar(50) = '',    -- 设置排序类型,不要加order by 
    @strWhere     varchar(2000) = ''  -- 查询条件 (注意: 不要加 where)
as 
BEGIN
    declare @Mycount varchar(2000)---此变量存储SQL语句,用于获得符合条件的记录总数
    declare @strSql varchar(2000)----此变量存储SQL语句,用于获得符合条件的记录
    if @OrderType=''
    set @OrderType='getdate()'

    if(@strWhere='')
    begin
        set @Mycount='select count(*) from '+@tblName
        set @strsql='select * from (select top ('+cast(@PageSize*@PageIndex as varchar(10))+') ROW_NUMBER()over(order by '+@OrderType+') rowNumber,'+@Fields+' from '+@tblName+')  t where rowNumber>'+cast((@PageIndex-1)*@PageSize as varchar(10))
    end
    else
    begin
        set @Mycount='select count(*) from '+@tblName+' where '+@strWhere
        set @strsql='select * from (select top ('+cast(@PageSize*@PageIndex as varchar(10))+') ROW_NUMBER()over(order by '+@OrderType+') rowNumber,'+@Fields+' from '+@tblName+' where '+@strWhere+')  t where rowNumber>'+cast((@PageIndex-1)*@PageSize as varchar(10))
    end  
        exec(@strSql)
        exec(@Mycount)
END

 

到此一个基本的分页功能就完成了

 需要源码的猛戳这里 http://files.cnblogs.com/justconnor/WebDiyer.rar

转载于:https://www.cnblogs.com/justconnor/archive/2012/07/07/2580179.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值