// 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>... ";
}
else
{
NewsData += "<li><a href='" + url + "?id=" + id + "'>" + ds.Tables[0].Rows[i]["title"].ToString() + "</a> ";
}
//输出日期
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) + "页 ";
if (CurrPage > 1)
{
NewsData += "<a href='?CurrPage=" + (CurrPage - 1) + "'>上一页</a> ";
}
if (CurrPage < PageCount)
{
NewsData += "<a href='?CurrPage=" + (CurrPage + 1) + "'>下一页</a>";
}
}
}
return NewsData;
}