我是靠谱客的博主 发嗲方盒,最近开发中收集的这篇文章主要介绍ASP.NET——Cookie对象实现零碎数据本地存储ASP.NET——Cookie对象实现零碎数据本地存储,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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>
            &nbsp;<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.NET——Cookie对象实现零碎数据本地存储ASP.NET——Cookie对象实现零碎数据本地存储所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(85)

评论列表共有 0 条评论

立即
投稿
返回
顶部