C#asp.net 制作静态HTML列表分页 另类办法做分页(不用控件)
因为最近弄个静态网站,需要列表页面都是静态的,这个时候网上的那些分页AspNetPager控件就没办法用了,用的话 你就没办法调取静态的值
思路:
比如读取一个栏目 ID为35的栏目; 给他设置查询值,然后每页查询12条数据,因为我这里设置每个页面是12条数据,这个ID35的栏目,总记录数量有350条;
那就可以分为10页来;按这个思路来弄就行了,分页控件也就不存在了;那我们就直接截取,下面因为是本地调试,所以连接上写了动态的,到时候使用的时候,直接改成静态连接地址就可以了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
public partial class list : System.Web.UI.Page
{
public string Get网站名称()////
{
string aa = 网站名称.Text; ///设置网址
return aa;
}
public string Get关键词()////
{
string aa = 子栏目.Text+","+关键词.Text; ///设置网址
return aa;
}
public string Get栏目简介()////
{
string aa = 栏目简介.Text; ///设置网址
return aa;
}
public string Get子栏目()////
{
string aa = 子栏目.Text; ///子栏目
return aa;
}
public string Get子栏目ID()////
{
string aa = 子栏目ID.Text; ///子栏目
return aa;
}
public string Get总记录数()////
{
string aa = 总记录数.Text;
return aa;
}
public string Get分页数量()////
{
string aa = 分页数量.Text;
return aa;
}
public string Get分页HTML()////
{
string aa = pageHTML.Text;
return aa;
}
public string Getchaxunid()////
{
string aa = chaxunid.Text;
return aa;
}
public string Get上一页()////
{
string aa = 上一页.Text;
return aa;
}
public string Get下一页()////
{
string aa = 下一页.Text;
return aa;
}
protected void Page_Load(object sender, EventArgs e)
{
string m = HttpContext.Current.Request.Url.Query;
string n = Regex.Match(m, "=").ToString();
if (n != "")
{
//存在
try
{
string cookie = HttpUtility.UrlDecode(Request.Cookies["usernamevip"].Value, Encoding.GetEncoding("utf-8"));
yonghuming.Text = cookie; // 显示用户名
logo.Visible = false;
huiyuan.Visible = true;
tuichu.Visible = true;
}
catch
{
logo.Visible = true;
huiyuan.Visible = false;
tuichu.Visible = false;
}
try
{
string url = HttpContext.Current.Request.Url.Query;
string getxt = url;
int iIndex = getxt.IndexOf("=");
string igetxt = getxt.Substring(0, iIndex);//提取=前的
int i = 1;
string duankou = getxt.Substring(iIndex + i++);
待分析.Text = duankou; ///得到 29&page=2
////////////////////////////////////////// 提取查询ID
string getxt1 = 待分析.Text;
int iIndex2 = getxt1.IndexOf("_");
string igetxt1 = getxt1.Substring(0, iIndex2);//
chaxunid.Text = igetxt1;
/////////////////////////////////////////////// 提取分页的数字
string getxt2 = 待分析.Text;
int iIndex3 = getxt2.IndexOf("_");
string igetxt2 = getxt2.Substring(0, iIndex);//
int vv = 1;
string yeshu = getxt2.Substring(iIndex3 + vv++);
当前页数.Text = yeshu;
}
catch
{
try
{
///// 假如他是栏目的第一页 ,就可能不存在 【 list.aspx?=29&page=2】这样的格式 应该是【list.aspx?=29】
string getxt = HttpContext.Current.Request.Url.Query;
int iIndex = getxt.IndexOf("=");
int i = 1;
string duankou = getxt.Substring(iIndex + i++);
chaxunid.Text = duankou;
当前页数.Text = "1";
}
catch
{
// Response.Write("<script>alert('该栏目ID不存在!');location.href='list.aspx?=1';</script>");
}
}
///// 这里是加载数据库 文章的ID
///////////////////////// 查询 每个ID 的库存 结果
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sql"].ConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("select count(*) from 文章列表 where 栏目ID = '" + chaxunid.Text + "'", conn);
object obj = cmd.ExecuteScalar();
if (obj != null)
{
this.总记录数.Text = obj.ToString();
double a = Convert.ToDouble(总记录数.Text);
double b = Convert.ToDouble(12); ///假设每页12条记录
double c = a / b;
分页数量.Text = "" + Math.Round(c,0);
conn.Close();
}
}
double a2 = Convert.ToDouble(当前页数.Text);
double b2 = Convert.ToDouble(1); ///上一页
double c2 = a2 - b2;
上一页.Text = "" + c2;
if (当前页数.Text == "1")
{
上一页.Text = "#";
}
////// 设置上一页
double a4 = Convert.ToDouble(当前页数.Text);
double b4 = Convert.ToDouble(1); ///上一页
double c4 = a4 + b4;
下一页.Text = "" + c4;
////// 设置下一页
double a1 = Convert.ToDouble(当前页数.Text);
double b1 = Convert.ToDouble(8); ///假设每页12条记录
double c1 = a1 + b1;
if (Convert.ToInt32(分页数量.Text) >= 5)
{
for (int i = Convert.ToInt32(当前页数.Text); i <= Convert.ToInt32(分页数量.Text); i++)
{
if (i >= c1)
{
break;
}
else
{
if(i== Convert.ToInt32(当前页数.Text))
{
pageHTML.Text += "<li>" + "<a class='active' href='./list" + Convert.ToInt32(chaxunid.Text) + "_" + i + ".html'>" + i + "</a></li>";
}
else
{
pageHTML.Text += "<li>" + "<a href='./list" + Convert.ToInt32(chaxunid.Text) + "_" + i + ".html'>" + i + "</a></li>";
}
}
}
}
else
{
for (int i = Convert.ToInt32(当前页数.Text); i <= Convert.ToInt32(分页数量.Text); i++)
{
pageHTML.Text += "<li>" + "<a href='list.aspx?=" + Convert.ToInt32(chaxunid.Text) + "_" + i + " ' >" + i + "</a></li>";
}
}
if (当前页数.Text == "1")
{
SqlConnection sqlCon1 = new SqlConnection(ConfigurationManager.ConnectionStrings["sql"].ConnectionString);
sqlCon1.Open(); //打开数据库连接 ///前台导航 免费
SqlCommand sqlcom1 = new SqlCommand(); //创建数据库命令对象
sqlcom1.CommandText = "select top 12 * from 文章列表 where 栏目ID = '" + chaxunid.Text + "' order by id"; //为命令对象指定执行语句
sqlcom1.Connection = sqlCon1; //为命令对象指定连接对象
this.Repeater1.DataSource = sqlcom1.ExecuteReader(); //为Repeater对象指定数据源
this.Repeater1.DataBind(); //绑定数据源
sqlCon1.Close(); //打开数据库连接
}
else
{
///假设每页12条记录
double a = Convert.ToDouble(当前页数.Text);
double b = Convert.ToDouble(12); ///假设每页12条记录
double c = a * b;
运算.Text = "" + c;
SqlConnection sqlCon1 = new SqlConnection(ConfigurationManager.ConnectionStrings["sql"].ConnectionString);
sqlCon1.Open(); //打开数据库连接 ///前台导航 免费
SqlCommand sqlcom1 = new SqlCommand(); //创建数据库命令对象
sqlcom1.CommandText = "select top(12) * from 文章列表 where id >= (select MAX(Id) from (select top(" + 运算.Text + ") * from 文章列表 where 栏目ID = '" + chaxunid.Text + "' order by id) as t)"; //为命令对象指定执行语句
sqlcom1.Connection = sqlCon1; //为命令对象指定连接对象
this.Repeater1.DataSource = sqlcom1.ExecuteReader(); //为Repeater对象指定数据源
this.Repeater1.DataBind(); //绑定数据源
sqlCon1.Close(); //打开数据库连接
}
SqlConnection sqlCon3 = new SqlConnection(ConfigurationManager.ConnectionStrings["sql"].ConnectionString);
sqlCon3.Open(); //打开数据库连接 ///前台导航 免费
SqlCommand sqlcom3 = new SqlCommand(); //创建数据库命令对象
sqlcom3.CommandText = "select * from 栏目"; //为命令对象指定执行语句
sqlcom3.Connection = sqlCon3; //为命令对象指定连接对象
this.Repeater4.DataSource = sqlcom3.ExecuteReader(); //为Repeater对象指定数据源
this.Repeater4.DataBind(); //绑定数据源
sqlCon3.Close(); //打开数据库连接
try
{
SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["sql"].ConnectionString);
con2.Open();
string tools2 = "select * from 栏目 where id='" + chaxunid.Text + "' ";
SqlCommand cmd2 = new SqlCommand(tools2, con2);
SqlDataReader dr2 = cmd2.ExecuteReader();
if (dr2.Read())
{
栏目简介.Text = dr2.GetString(dr2.GetOrdinal("栏目简介"));
子栏目.Text = dr2.GetString(dr2.GetOrdinal("子栏目"));
子栏目ID.Text = dr2[0].ToString().Trim(); ///这里是读int 类型的
}
con2.Close();
}
catch
{
}
SqlConnection con3 = new SqlConnection(ConfigurationManager.ConnectionStrings["sql"].ConnectionString);
con3.Open();
string tools13 = "select * from 网站设置";
SqlCommand cmd3 = new SqlCommand(tools13, con3);
SqlDataReader dr3 = cmd3.ExecuteReader();
if (dr3.Read())
{
网站名称.Text = dr3.GetString(dr3.GetOrdinal("网站名称"));
关键词.Text = dr3.GetString(dr3.GetOrdinal("关键词"));
}
con3.Close();
}
else
{
//不存在
Response.Write("<script>alert('该栏目ID不存在!');location.href='list.aspx?=29';</script>");
}
}
}
前台代码:
<title><%=Get子栏目()%>_<%=Get网站名称()%></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="keywords" content="<%=Get关键词()%>" />
<meta name="description" content="<%=Get栏目简介()%>" />
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="list.aspx.cs" Inherits="list" %>
<body>
<form id="form1" runat="server">
<div>
<!--#include file="调用模板/list.html"-->
<asp:Label ID="网站名称" runat="server" Text="" style="display:none" Visible ="false"></asp:Label>
<asp:Label ID="关键词" runat="server" Text="" style="display:none" Visible ="false"></asp:Label>
<asp:Label ID="栏目简介" runat="server" Text="" style="display:none" Visible ="false"></asp:Label>
<asp:Label ID="子栏目" runat="server" Text="" style="display:none" Visible ="false"></asp:Label>
<asp:Label ID="顶级栏目" runat="server" Text="" style="display:none" Visible ="false"></asp:Label>
<asp:Label ID="子栏目ID" runat="server" Text="" style="display:none" Visible ="false"></asp:Label>
总记录数: <asp:Label ID="总记录数" runat="server" Text="0"></asp:Label>
分页数量: <asp:Label ID="分页数量" runat="server" Text="0"></asp:Label>
待分析: <asp:Label ID="待分析" runat="server" Text="0" ></asp:Label>
查询ID: <asp:Label ID="chaxunid" runat="server" Text="15" ></asp:Label>
当前页数: <asp:Label ID="当前页数" runat="server" Text="0" ></asp:Label>
计算: <asp:Label ID="运算" runat="server" Text="0" ></asp:Label>
<asp:label runat="server" text="关键词" id="woguanjianci" style="display:none" ></asp:label>
<asp:Label ID="上一页" runat="server" Text="" style="display:none" Visible ="false"></asp:Label>
<asp:Label ID="下一页" runat="server" Text="" style="display:none" Visible ="false"></asp:Label>
<asp:Label ID="pageHTML" runat="server" Text="" ></asp:Label>
</form>
</body>
</html>