asp.net 自定义分页

本文介绍了一个基于ASP.NET的后台下载管理页面实现方案,包括文件的搜索、展示及简单的分页功能。通过C#代码展示了如何从数据库中读取下载文件信息并将其显示在网页上。

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="admin_down_manage.aspx.cs" Inherits="admin_zzyjs_editor_administrators_admin_down_manage" %>

<!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 runat="server">
    <title>无标题页</title>
    <script src="js/admin_pro.js"></script>
    <link href="images/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
        <table width="98%" border="0" cellpadding="0" cellspacing="0" style="text-align:center;">
            <tr>
                <td style="height:10px;"></td>
            </tr>
        </table>
        <table width="98%" border="0" cellpadding="0" cellspacing="0" style="text-align:center;">
            <tr>
                <td><span style="width:95px; height:20px; float:left; background:url(images/admin_18.gif) no-repeat; padding-top:6px;" class="left">下载管理</span></td>
            </tr>
            <tr>
                <td align=center style=" padding:10px; background:#FEF8FA;">
                    <span style="width:83px; height:35px; margin-bottom:5px; float:left; background-image:url(images/batton.gif); line-height:35px;"><a href="admin_down_add.aspx" class="batton">添加文件</a></span>
                    <span style="float:left; width:600px; padding-top:10px; text-align:left; padding-left:10px;">
                     请输入关键词:<asp:TextBox ID="txtKeyword" runat="server" MaxLength="50" Width="300px"></asp:TextBox>
                     <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="搜索" />
                    </span>
                    <asp:Repeater ID="Repeater1" runat="server">
                        <HeaderTemplate>
                        <table width="100%" border="0" cellpadding="0" cellspacing="0" class="table">
                            <tr>
                               <td class="hback" style="height:33px; text-align:center; width:40px; background:#F1D4D8;">&nbsp;</td>
                               <td class="hback" style="text-align:center; width:60px; background:#F1D4D8; height: 33px;">排序号</td>
                               <td class="hback" style="text-align:center; width: 400px; background:#F1D4D8; height: 33px;">专业名称</td>
                               <td class="hback" style="text-align:center; width: 150px; background:#F1D4D8; height: 33px;">状态</td>
                               <td class="hback" style="text-align:center; background:#F1D4D8; height: 33px;">操作</td>
                            </tr>                       
                        </HeaderTemplate>
                        <ItemTemplate>
                            <tr onMouseOver="overColor(this)" onMouseOut="outColor(this)">
                               <td class="hback" style="height:33px; text-align:center; width:40px;"><input id="Down" name="Down" runat="server" type="checkbox" value='<%# Eval("ID") %>' /></td>
                               <td class="hback" style="text-align:center; width:60px; height: 33px;"><%# Eval("ID") %></td>
                               <td class="hback" style="text-align:left; width: 400px; height: 33px; padding-left:10px"><%# Eval("DownTitle") %></td>
                               <td class="hback" style="text-align:center; width: 150px; ; height: 33px;">
                                <img src="images/shen<%# Eval("Review").ToString()=="1"?"1":"2" %>.gif" />&nbsp;
                                <img src="images/jian<%# Eval("Rec").ToString()=="1"?"1":"2" %>.gif" />&nbsp;
                                <img src="images/ding<%# Eval("TTop").ToString()=="1"?"1":"2" %>.gif" />&nbsp;
                                <img src="images/xian<%# Eval("Hidden").ToString()=="1"?"1":"2" %>.gif" />
                               </td>
                               <td class="hback" style="text-align:center; height: 33px;">修改 删除</td>
                            </tr>                       
                        </ItemTemplate>
                        <FooterTemplate>
                        </table>
                        </FooterTemplate>
                    </asp:Repeater>
                 共有<asp:Label id="lblRecordCount" ForeColor="red" runat="server" />条记录 
                  当前为<asp:Label id="lblCurrentPage" ForeColor="red" runat="server" />/<asp:Label id="lblPageCount" ForeColor="red" runat="server" />页 
                <asp:LinkButton id="lbnFirstPage" Text="首  页" CommandName="first" OnCommand="Page_onClick" runat="server" />
                <asp:LinkButton id="lbnPrevPage" Text="上一页" CommandName="prev" OnCommand="Page_onClick" runat="server" />
                <asp:LinkButton id="lbnNextPage" Text="下一页" CommandName="next" OnCommand="Page_onClick" runat="server" />
                <asp:LinkButton id="lbnEndPage" Text="尾  页" CommandName="endd" OnCommand="Page_onClick" runat="server" />               
                </td>
            </tr>
        </table>
      </form>
</body>
</html>
/*----------------自定义分页 cs-----------------*/

 

using System;
using System.Data;
using System.Data.SqlClient;
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;

public partial class admin_down_manage : System.Web.UI.Page
{
    Const webconst = new Const();
    SQLConst down = new SQLConst();
    SqlConnection conn;
    //int curPage, pageSize, recordCount, pageCount;
    int PageSize, RecordCount, PageCount, CurrentPage,pageCount;
    protected void Page_Load(object sender, EventArgs e)
    {
        //连接打开数据库
        string sql = "select * from zzy_Downs order by id desc";
        //将数据绑定在控件Repeater1上面
        Repeater1.DataSource = down.getSql(sql);
        Repeater1.DataBind();

        PageSize = 10;
        ListBind();
        CurrentPage = 0;
        if (!Page.IsPostBack)       //第一次请求执行
        {
            ViewState["PageIndex"] = 0; //计算总共有多少记录
            RecordCount = CalculateRecord();
            lblRecordCount.Text = RecordCount.ToString(); //计算总共有多少页
            PageCount = RecordCount / PageSize + 1;
            lblPageCount.Text = PageCount.ToString();
            ViewState["PageCount"] = PageCount;
        }
     }
     //计算总共有多少条记录
     public int CalculateRecord()
     {
         SqlConnection conn = new SqlConnection(webconst.connstr);
         conn.Open();
         int intCount;
         string strCount = "select Count(ID) As ID from zzy_Downs order by id desc";
         SqlCommand MyComm = new SqlCommand(strCount, conn);
         intCount = Convert.ToInt32(MyComm.ExecuteScalar());
         return intCount;
     }

     ICollection CreateSource()
     {

         int StartIndex;
         SqlConnection conn = new SqlConnection(webconst.connstr);
         conn.Open();

         //设定导入的起终地址
         StartIndex = CurrentPage * PageSize;
         string strSel = "select * from zzy_Downs order by id desc";
         DataSet ds = new DataSet();

         SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, conn);
         MyAdapter.Fill(ds, StartIndex, PageSize, "zzy_Downs");

         return ds.Tables["zzy_Downs"].DefaultView;
     }
     public void ListBind()
     {
         this.Repeater1.DataSource = CreateSource();
         this.Repeater1.DataBind();

         lbnFirstPage.Enabled = true;
         lbnNextPage.Enabled = true;
         lbnPrevPage.Enabled = true;
         lbnEndPage.Enabled = true;
         if (CurrentPage == (PageCount - 1)) lbnNextPage.Enabled = false;
         if (CurrentPage == 0) lbnPrevPage.Enabled = false;
         lblCurrentPage.Text = (CurrentPage + 1).ToString();
     }
     public void Page_onClick(Object sender, CommandEventArgs e)
     {
         CurrentPage = (int)ViewState["PageIndex"];
         PageCount = (int)ViewState["PageCount"];

         string cmd = e.CommandName;
         //判断cmd,以判定翻页方向
         switch (cmd)
         {
             case "next":
                 if (CurrentPage < (PageCount - 1)) CurrentPage++;
                 break;
             case "prev":
                 if (CurrentPage > 0) CurrentPage--;
                 break;
             case "endd":
                 if (CurrentPage < (PageCount - 1)) CurrentPage = PageCount - 1;
                 break;
             case "first":
                 if (CurrentPage > 0) CurrentPage = 0;
                 break;
         }

         ViewState["PageIndex"] = CurrentPage;
         ListBind();
     }


     protected void Button2_Click(object sender, EventArgs e)
    {
        //绑定查询结果

        string keyword = txtKeyword.Text;
        string sql = "select * from zzy_Downs where (DownTitle like '%" + keyword + "%' or sDownTitle like '%" + keyword + "%')";
        Repeater1.DataSource = down.getSql(sql);
        Repeater1.DataBind();
    }

}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值