温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何编写.NET 纯分页代码

发布时间:2021-10-09 11:41:43 来源:亿速云 阅读:101 作者:iii 栏目:开发技术

本篇内容主要讲解“如何编写.NET 纯分页代码”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何编写.NET 纯分页代码”吧!

前台

复制代码 代码如下:

<div class="mydiv"   >
             <div >
                   <div ><span >即将过期账册提醒>></span></div>
                   <div >
                       <asp:ScriptManager ID="ScriptManager1" runat="server">
                       </asp:ScriptManager>
                           <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                               <ContentTemplate>
                                    <asp:Repeater ID="repZC" runat="server">
    <HeaderTemplate>
      <table border="1"   cellpadding="0" cellspacing="0" >
      <tr>
        <td >账册名称</td>
        <td >经营单位</td>
        <td >距过期天数</td>
      </tr>
    </HeaderTemplate>
    <ItemTemplate>
      <tr>
        <td><%# DataBinder.Eval(Container.DataItem, "ems_no")%></td>
        <td><%# DataBinder.Eval(Container.DataItem, "TRADE_NAME")%></td>
        <td ><%# DataBinder.Eval(Container.DataItem, "days")%></td>
      </tr>
    </ItemTemplate>
    <%--AlternatingItemTemplate描述交替输出行的另一种外观--%>
    <AlternatingItemTemplate>
      <tr bgcolor="#e8e8e8">
        <td><%# DataBinder.Eval(Container.DataItem, "ems_no")%></td>
        <td><%# DataBinder.Eval(Container.DataItem, "TRADE_NAME")%></td>
        <td ><%# DataBinder.Eval(Container.DataItem, "days")%></td>
      </tr>
    </AlternatingItemTemplate>
    <FooterTemplate>
     </table>
    </FooterTemplate>
                       </asp:Repeater>
       <div >
            <asp:LinkButton ID="lbFirst" runat="server"    CommandArgument="F" OnCommand="lbPage_Command">首页</asp:LinkButton>
            <asp:LinkButton ID="lbPre" runat="server"   CommandArgument="P"  OnCommand="lbPage_Command">上页</asp:LinkButton>
            <asp:LinkButton ID="lbNext" runat="server"   CommandArgument="N"  OnCommand="lbPage_Command">下页</asp:LinkButton>
            <asp:LinkButton ID="lbLast" runat="server"   CommandArgument="L"  OnCommand="lbPage_Command">末页</asp:LinkButton>
            &nbsp;
            <asp:Label ID="lbTip"   runat="server" Text="Label"></asp:Label>
        </div>        
                               </ContentTemplate>
                           </asp:UpdatePanel>
                   </div>
              </div>
             </div>


分页类

复制代码 代码如下:

[Serializable]
    public class Pager
    {
        public int totalRecords;
        public int pageSize;
        public int pageIndex;
        public int totalPages;
    }


后他代码

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InfoSoftGlobal;
using System.Data;
using DCIS.Persistence;
using DCIS.JDXT.Data;
using System.IO;
public partial class COMMON_FirstPage : System.Web.UI.Page
{
    DCIS.Web.WebUserInfo CurrentUser = new DCIS.Web.WebUserInfo();
    protected void Page_Load(object sender, EventArgs e)
    {
        Pager pager = new Pager();
        CurrentUser = (DCIS.Web.WebUserInfo)Session["CurrentUser"];
        if (!IsPostBack)
        {
            BindDoc();
            object o = DBUtil.ExecuteScalarAt("SQLCOM", "select count(1) from dbo.EMS_PTS_HEAD where DECLARE_CODE='" + CurrentUser.Extends["AGENT_CODE"].ToString() + "' and datediff(day,end_Date,getdate()) between 0 and 7");
            pager.totalRecords = o == null ? 0 : Convert.ToInt32(o);//总记录数
            pager.pageSize = 5;//页大小
            pager.pageIndex = 0;//当前页码
            if (pager.totalRecords == 0)
            {
                pager.totalPages = 0;
            }
            else
            {
                pager.totalPages = pager.totalRecords / pager.pageSize + (pager.totalRecords % pager.pageSize == 0 ? 0 : 1)-1;//总页数
            }
            ViewState["pager"] = pager;
            litChart.Text = CreatChart();
            bindZC(pager.pageIndex, pager.pageSize);
            viewLB(pager);
        }
    }
            private void bindZC(int pageIndex,int pageSize) {
        string strSQL = @"select EMS_NO,TRADE_NAME,datediff(day,end_Date,getdate()) DAYS from dbo.EMS_PTS_HEAD where DECLARE_CODE='" + CurrentUser.Extends["AGENT_CODE"].ToString() + "' and datediff(day,end_Date,getdate()) between 0 and 7";
        PageSpliter pageSpliter = new PageSpliter(strSQL, ""/*在此配置排序子句*/, "SQLCOM");
        pageSpliter.PageSize = pageSize;
        strSQL = pageSpliter.GetPageSQL(pageIndex);
        strSQL +=" order by days ";
        DataTable dt = DBUtil.FillAt("SQLCOM", strSQL);
        repZC.DataSource = dt;
        repZC.DataBind();
    }
    public void lbPage_Command(object sender, CommandEventArgs e)
    {
       Pager pager =ViewState["pager"] as Pager ;
        string para = e.CommandArgument.ToString();
        //首页
        if (para == "F") {
            pager.pageIndex = 0;
        }
        //前一页
        if (para == "P")
        {
            pager.pageIndex = pager.pageIndex-1;
        }
        //下一页
        if (para == "N")
        {
            pager.pageIndex = pager.pageIndex+1;
        }
        //尾页
        if (para == "L")
        {
            pager.pageIndex = pager.totalPages;
        }
        bindZC(pager.pageIndex, pager.pageSize);
        viewLB(pager);
        ViewState["pager"] = pager;
    }
    private void viewLB(Pager pager)
    {
        lbFirst.Enabled = true;
        lbLast.Enabled = true;
        lbPre.Enabled = true;
        lbNext.Enabled = true;
        if (pager.totalPages == 0)
        {
            lbFirst.Enabled = false;
            lbLast.Enabled = false;
            lbPre.Enabled = false;
            lbNext.Enabled = false;
        }
        if (pager.pageIndex == 0)
        {
            lbFirst.Enabled = false;
            lbPre.Enabled = false;
        }
        if (pager.totalPages == pager.pageIndex)
        {
            lbLast.Enabled = false;
            lbNext.Enabled = false;
        }
        lbTip.Text = pager.pageIndex + 1 + "/" + (pager.totalPages + 1) + "页";
    }
    /// <summary>
    /// 绑定文档
    /// </summary>
    private void BindDoc() {
        DataTable dt = DBUtil.FillAt("SQLCOM"," select * from COM_FileUpload WHERE DESCRIBE='Index'");
        repMD.DataSource = dt;
        repMD.DataBind();
    }
    public void DownFile_Command(object sender, CommandEventArgs e)
    {
        string para = e.CommandArgument.ToString();
        string serverfilpath=Server.MapPath("../" + para.Split('$')[0]);
        if (!File.Exists(serverfilpath))
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('你要下载的文件已经不存在!');</script>");
            return;
        }
        ToDownload(serverfilpath,para.Split('$')[1]);
    }
    public static void ToDownload(string serverfilpath, string filename)
    {
        FileStream fileStream = new FileStream(serverfilpath, FileMode.Open);
        long fileSize = fileStream.Length;
        HttpContext.Current.Response.ContentType = "application/octet-stream";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + UTF_FileName(filename) + "\";");
        ////attachment --- 作为附件下载
        ////inline --- 在线打开
        HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
        byte[] fileBuffer = new byte[fileSize];
        fileStream.Read(fileBuffer, 0, (int)fileSize);
        HttpContext.Current.Response.BinaryWrite(fileBuffer);
        fileStream.Close();
        HttpContext.Current.Response.End();
    }
    private static string UTF_FileName(string filename)
    {
        return HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);
    }
}

到此,相信大家对“如何编写.NET 纯分页代码”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI