c# asp.net 自己写的分页 没有使用控件原始分页

// c# asp.net 自己写的分页  没有使用控件原始分页
  

//新闻列表页+分页  
    public string news_all_list(string type, int length, int showtype,int cur,string url)
    {
        //type 新闻类型 num 显示条数 length 单条字数 showtype 显示形式
        string NewsData = "";
        int CurrPage = 0;
        int everyPage = 5; //每页多少条记录
        int PageCount = 0;  //总页数
        //CurrPage = Convert.ToInt32(Request.QueryString["CurrPage"]);
        CurrPage=cur; //当前页
        if (type != null && length != null && showtype != null && url!=null)
        {
            string sql = "select id,title,hide,content,releaseDate,availableDate from news_info where cagid='" + type + "'";
            string sql2 = "select count(*) from news_info where cagid='" + type + "'";
            System.Data.DataSet ds2 = ukt.Select_Table_ReDS(sql2);
            if (ds2.Tables[0].Rows.Count > 0)
            {
                int num = Convert.ToInt32(ds2.Tables[0].Rows[0][0]);
                int sum = 0;
                sum = num / everyPage;    //页数
                if (num % everyPage == 0)  //是否余数
                {
                    PageCount = sum;
                }
                else
                {
                    PageCount = sum + 1;
                }
            }
            //分页限制
            if (CurrPage < 1)
            {
                CurrPage = 1;
            }
            else
            {
                if (CurrPage > PageCount)
                {
                    CurrPage = PageCount;
                }
                else
                {
                    CurrPage = CurrPage;
                }
            }
            System.Data.DataSet ds = ukt.Select_Table_ReDS(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                int j = 0;
                if (CurrPage == 1)
                {
                    j = 1;  //取得初始变量
                }
                else
                {
                    // j = everyPage * CurrPage - 1;    
                    j = everyPage * CurrPage - everyPage + 1;     //改变初始变量
得到循环次数
                }
                for (int i = j - 1; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (Convert.ToDateTime(ds.Tables[0].Rows[i]["availableDate"]) > System.DateTime.Now && Convert.ToInt32(ds.Tables[0].Rows[i]["hide"]) == 1)
                    {
                        int id = int.Parse(ds.Tables[0].Rows[i]["id"].ToString());
                        if (ds.Tables[0].Rows[i]["title"].ToString().Length > length)
                        {
                            NewsData += "<li><a href='"+url+"?id=" + id + "'>" + ds.Tables[0].Rows[i]["title"].ToString().Substring(0, length) + "</a>...&nbsp;&nbsp;&nbsp;&nbsp;";
                        }
                        else
                        {
                            NewsData += "<li><a href='" + url + "?id=" + id + "'>" + ds.Tables[0].Rows[i]["title"].ToString() + "</a>&nbsp;&nbsp;&nbsp;&nbsp;";
                        }
                        //输出日期
                        if (showtype == 1)
                        {
                            NewsData += Convert.ToDateTime(ds.Tables[0].Rows[i]["releaseDate"]).ToShortDateString() + "</li><br>";
                        }
                        else if (showtype == 2)
                        {
                            NewsData += Convert.ToDateTime(ds.Tables[0].Rows[i]["releaseDate"]).ToString("MM-dd") + "</li><br>";
                        }
                        else
                        {
                            NewsData += "</li><br>";
                        }
                    }
                    if (i + 1 > everyPage * CurrPage - 1)
                    {
                        break;    //控制结束循环

                    }

                }
                NewsData += "<br>当前页" + (CurrPage) + "共" + (PageCount) + "页&nbsp;&nbsp;&nbsp;&nbsp;";
                if (CurrPage > 1)
                {
                    NewsData += "<a href='?CurrPage=" + (CurrPage - 1) + "'>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;";
                }
                if (CurrPage < PageCount)
                {
                    NewsData += "<a href='?CurrPage=" + (CurrPage + 1) + "'>下一页</a>";
                }
            }

        }
        return NewsData;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值