标签:
分页,cookies,session用法
一:获取分页
public partial class RNewsM : System.Web.UI.Page
{
private int PageCount = 0; //总共几页
private int PageSize = 10; //一页多少条数据
private int _pageIndex; //当前页
public int PageIndex
{
get
{
try
{
_pageIndex = Request.QueryString["page"] == null ? 1 : Convert.ToInt32(Request.QueryString["page"].ToString());
}
catch
{
_pageIndex = 1;
}
return _pageIndex;
}
set { _pageIndex = value; }
}
public int GetRecordCount() //总数据
{
string sqlStr = "select count(*) from RNews";
int i = Convert.ToInt32(SqlHelper.ExecuteScalar(CommandType.Text, sqlStr, null));
return i;
}
public void BindRNews()
{
分页查询sql语句
select top 页大小 *
from
(
select ROW_NUMBER() over(order by id)as RowNumber,* from table1 ///over获取结果集 ROW_NUMBER() over 固定写法,获取记录数
)A
where RowNumber > 页大小*(页数-1)
StringBuilder sb = new StringBuilder();
sb.Append(string.Format(@"SELECT TOP {0} * FROM (
SELECT ROW_NUMBER() OVER (ORDER BY NewsId) AS RowNumber,NewsId,Title,CreatedTime,NewsClass,ViewCount FROM RNews where 1=1", PageSize));
SqlParameter[] para = new SqlParameter[2];
if (!string.IsNullOrEmpty(txtSRNewsId.Text.Trim()))
{
sb.Append(" and NewsId=@NewsId");
para[0] = new SqlParameter("@NewsId", SqlDbType.Int);
para[0].Value = Convert.ToInt32(txtSRNewsId.Text.Trim());
}
if (ddlSNewsClass.SelectedIndex > 0)
{
sb.Append(" and NewsClass=@NewsClass");
para[1] = new SqlParameter("@NewsClass", SqlDbType.NVarChar, 50);
para[1].Value = ddlSNewsClass.SelectedValue;
}
sb.Append(string.Format(@" ) A
WHERE RowNumber > {0}*({1}-1)", PageSize, PageIndex));
//SqlDataReader read = SqlHelper.ExecuteReader(SqlHelper.connectionString, CommandType.Text, sb.ToString(), para);
DataTable dt = SqlHelper.ExecuteDataSetText(sb.ToString(), para).Tables[0];
//gvRNews.DataSource = read;
gvRNews.DataSource = dt;
gvRNews.DataBind();
}
/// <summary>
/// 获取分页
/// </summary>
/// <returns></returns>
public string GetPagerHtml()
{
StringBuilder sb = new StringBuilder();
sb.Append(@"<div><a href=""/RNewsM.aspx?page=1"">首页</a></div>");
if (PageIndex == 1)
{
sb.Append(@"<div class=""nonbg"">上一页</div>");
}
else
{
sb.Append(string.Format(@"<div><a href=""/RNewsM.aspx?page={0}"">上一页</a></div>", PageIndex - 1));
}
if (GetRecordCount() % PageSize == 0)
{
PageCount = GetRecordCount() / PageSize;
}
else
{
double d = GetRecordCount() / PageSize;
PageCount = Convert.ToInt32(Math.Ceiling(d));
}
if (PageIndex == PageCount)
{
sb.Append(@"<div class=""nonbg"">下一页</div>");
}
else
{
sb.Append(string.Format(@"<div><a href=""/RNewsM.aspx?page={0}"">下一页</a></div>", PageIndex + 1));
}
sb.Append(string.Format(@"<div><a href=""/RNewsM.aspx?page={0}"">尾页</a></div>", PageCount));
sb.Append(string.Format(@"<div class=""nonbg"">{0}/{1}</div>", PageIndex, PageCount));
return sb.ToString();
}
}
二:session用法
客户端发送一个请求到服务端,在session池通过key返回客户端响应请求
请求信息(请求报文):Request
响应信息(响应报文):Response
请求报文+响应报文=上下文对象 context
session 返回的key是储存在客户端浏览器的缓存里面的,所以,一旦页面关闭后,key就不在了,session就取不到了
在登录页面中:
if (SqlHelper.Exists(strSql, para))//判断sql语句里面的结果是否存在
{
Session["username"] = username; //用户名保存到session中
Response.Redirect("RNewsM.aspx");
}
else
{
Response.Write("<script>alert(‘用户名或者密码错误‘);</script>");
}
在跳转页面里:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null) //如果session为空,返回登录页
{
Response.Redirect("Login.aspx");
}
if (!IsPostBack) //页面加载之后
{
BindRNews();
}
}
三:cookies用法
客户端向服务端发送请求,服务端返回cookies的值给客户端,默认情况下(没有设置过期时间),保存在浏览器的缓存里面。
如果cookies设置一个过期时间,cookies保存在客户端的硬盘上(一般在c盘),直到过期时间达到,或者手动去删除cookies,它才会消失。
登录页面:
if (SqlHelper.Exists(strSql, para))//判断sql语句里面的结果是否存在
{
//if (chk.Checked)
//{
//Response.Cookies["username"].Expires = DateTime.Now.AddHours(1);
/////在前台页面加入选择框:<asp:CheckBox ID="chk" runat="server" />,在后台设置过期时间 如:AddHours(1)为一小时后过期,年月日都可以设置
//}
Response.Cookies["username"].Value = username; //保存到cookies中
Response.Redirect("RNewsM.aspx");
}
else
{
Response.Write("<script>alert(‘用户名或者密码错误‘);</script>");
}
在跳转页面:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["username"] == null) //判断cookies为空时,返回登录页
{
Response.Redirect("Login.aspx");
}
}
标签:
原文地址:http://www.cnblogs.com/71yishen/p/5037204.html