程序中保存状态的方式之Cookies
程序中保存状态的方式之 Cookies,之前写过一篇关于ViewState的。现在继续总结Cookies方式的
新建的测试页面login
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function checkSubmit() {
var user = document.getElementById("txtName");
var reg = /^\s*$/;
if (reg.test(user.value)) {
alert("请输入用户名!");
user.focus();
return false;
}
var pwd = document.getElementById("txtPwd");
if (reg.test(pwd.value)) {
alert("请输入密码!");
pwd.focus();
return false;
} return true;
}
</script>
</head>
<body>
<form id="form1" runat="server"> 登录名:<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
密码:<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox>
<asp:Button ID="btn_login" runat="server" Text="登录" onclick="btn_login_Click" OnClientClick="return checkSubmit()"/>
</form>
</body>
</html>
login.aspx
后台.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data; public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void btn_login_Click(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
string pwd = txtPwd.Text.Trim();
if (name == string.Empty)
{ MessageBox.Show(this, "请输入用户名!");
return;
}
if (pwd == string.Empty)
{ MessageBox.Show(this, "请输入密码!");
return;
} if (name == "test" && pwd == "")
{
Response.Cookies.Add(new HttpCookie("comID", ""));
Response.Cookies["comID"].Expires = DateTime.Now.AddDays();//设置过期时间,30天
Response.Redirect("获取cookies.aspx");
}
else {
MessageBox.Show(this,"用户名密码错误!");
} } }
获取cookies.aspx页面后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data; public partial class 获取cookies : System.Web.UI.Page
{
public string comid = "";
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["comID"] != null && Request.Cookies["comID"].Value != "")
{
comid = Request.Cookies["comID"].Value;//获取存入的comid
}
MessageBox.Show(this, "登录id=" + comid);
}
}
删除Cookies
Response.Cookies[CountAdmin].Expires = DateTime.Now.AddDays(-1);//删除cookie
我登录的时候存入了100,访问login.aspx就会跳转到获取cookies页面,提示登录id=100,效果图如下:

程序中保存状态的方式之Cookies的更多相关文章
- 程序中保存状态的方式之ViewState
程序中保存状态的方式有以下几种: 1.Application 2.Cookie 3.Session 4.ViewState:ViewState是保存状态的方式之一,ViewState实际就是一个Hid ...
- 在C 函数中保存状态:registry、reference和upvalues
在C函数中保存状态:registry.reference和upvalues C函数能够通过堆栈来和Lua交换数据,但有时候C函数须要在函数体的作用域之外保存某些Lua数据.那么我们想到全局变 ...
- 在 DotNetCore 3.0 程序中使用通用协议方式启动文件关联应用
问题描述 在传统的基于 .NET Framework 的 WPF 程序中,我们可以使用如下代码段启动相关的默认应用: # 启动默认文本编辑器打开 helloworld.txt Process.Star ...
- 在c中保存状态
1. 注册表 注册表是一个普通的table,我们可以将c函数中需要保存的状态都存储在注册表中,注册表是可以被多个c模块共享的. 由于注册表是一个普通table,我们同样可以在栈中对其进行操作,只是这个 ...
- 在C函数中保存状态:registry、reference和upvalues
C函数可以通过堆栈来和Lua交换数据,但有时候C函数需要在函数体的作用域之外保存某些Lua数据,那么我们想到全局变量或static变量,这样做的缺点是:(1)为Lua设计C函数库时,导致不可重入:(2 ...
- Spring MVC不要在@Service bean中保存状态
先看这么一段代码: @Service public class AccountService { private String message; public void foo1() { if (tr ...
- LUA 在C函数中保存状态:registry、reference
1 背景 lua的值一般都是保存在栈里面,调用函数完毕值在栈会被清掉,从而被GC回收.但有时候C函数需要在函数体的作用域之外保存某些Lua数据,这些数据不能存放在栈里面,有没有全局变量之类的可以存放. ...
- (11)Web程序保存状态的几种方式,Application,Session,Cookie,ViewState
WEb程序保存状态的方式有这样几种: 1.Application:保存在Application中的数据是全局有效的:Application里面存放的应该是访问多修 改较少并且是全局至少大部分 ...
- 向Hive中传入变量的方式
Hive向程序中传递变量的方式 暴力替换 字符串替换 正则替换 模板引擎 系统环境变量 shell环境变量:${env:varname} system系统变量:${system:varname} hi ...
随机推荐
- python 爬虫(五)
下载媒体文件 I 使用urllib.request.urlretrieve方法可以下载文件存为指定文件 from urllib.request import urlretrieve from urll ...
- 关于WORD文档的读取乱码问题
一直以来都是用File类操作txt文档,今天想尝试能不能打开word文档,无奈,尝试了UTF8,Unicode,Default....等编码方式,打开文件都是乱码,电脑甚至发出警报声. 以下只取一种编 ...
- 使EditText不要在页面打开时自动获取焦点(因为软键盘会自动弹出)
当页面上有EditText时,第一个EditText会在页面打开时自动的获取焦点,这样带来的问题就是系统键盘会自动的弹出来. 解决方法比较简单只要为EditText的父控件设置一下就行了: andro ...
- bzoj3631树链剖分
虽然是水题1A的感觉太爽了O(∩_∩)O~ 题意相当于n-1次树上路径上每个点权值+1,最后问每个点的权值 本来想写线段树,写好了change打算框架打完了再来补,结果打完发现只是区间加和单点查 前缀 ...
- JavaScript解惑记之Array.prototype.sort()
前言 看JS红宝书的5.2.5章节关于sort()方法,如何用一个compare函数,让数组顺序,倒序,有点云里雾里的.在网上度娘了一下,发现更迷糊了.走投无路的情况下,只能发动神技能,去 stack ...
- JAVA_javax.net.ssl.SSLProtocolException: handshake alert: unrecognized_name
tomcat访问https请求返回: javax.net.ssl.SSLProtocolException: handshake alert: unrecognized_name at sun.se ...
- 在update语句中使用子查询
在update 中的 where 子句中使用子查询: UPDATE mg_page_log as a SET page_num=1 WHERE id in( SELECT id from mg_ ...
- 有哪些LabVIEW快捷键让你相见恨晚
前言 任何一门工具,当你使用它到极致的时候,往往都朝着这样子的两个方向发展 1. 具有鼠标操作功能的软件,他的使用的极致就是脱离鼠标,迈向键盘 2. 主要是键盘操作的,他的极致就是脚本自动化,或者说一 ...
- 猥琐的wordpress后门分享
https://www.t00ls.net/thread-37312-1-1.html 一个可以自动调用管理员帐号登录wordpress后台的方法. <?php require('../../. ...
- 用c#创建支持多语言的WinForm应用程序
实现多语言的方法可能有使用资源文件,或者配置xml两种方法吧.没时间研究过多,学习了一下使用资源文件的方法,成功了. 在.net2.0 中,m$ 为我们提供了一种简单方便的方法, 使用资源文件 1.新 ...