ASP.NET——Cookie对象实现零碎数据本地存储
案例代码
前端代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASP.NET_Demo2.Demo05.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
请输入用户名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<asp:Button ID="btnSaveToCookie" runat="server" OnClick="btnSaveToCookie_Click"
Text="将用户名保存到Cookie" />
</div>
</form>
</body>
</html>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//取出Cookie值
if (Request.Cookies["UserName"] != null)
Response.Write("用户名=" + Request.Cookies["UserName"].Value);
if (Request.Cookies["UserPhone"] != null)
Response.Write("用户电话=" + Request.Cookies["UserPhone"].Value);
}
}
protected void btnSaveToCookie_Click(object sender, EventArgs e)
{
//方法一:保存Cookie并设置有效期
Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(1.0);
Response.Cookies["UserName"].Value = this.txtUserName.Text.Trim();
//方法二:
HttpCookie hcookie = new HttpCookie("UserPhone", "12345678");
hcookie.Expires = DateTime.Now.AddDays(1.0);
Response.Cookies.Add(hcookie);
}
最后
以上就是发嗲方盒最近收集整理的关于ASP.NET——Cookie对象实现零碎数据本地存储ASP.NET——Cookie对象实现零碎数据本地存储的全部内容,更多相关ASP内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复