程序中保存状态的方式之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 ...
随机推荐
- 【APICloud】利用sublimetext3编写apicloud
下载sublime text 3 安装插件 使用模糊搜索apicloud有三个插件全部下载下来 安装海马玩模拟器,这是一个安卓的模拟器,进入官网下载后直接安装就可以了. 打开sublime text ...
- Apple、Google、Microsoft的用户体验设计原则
轻巧的Apple 注重设计过程: 在设计过程中引入用户交互的5个目标: 了解您的目标客户 分析用户的工作流 构造原型系统 观察用户测试 制定观察用户准则 做出设计决定 避免功能泛滥 80% 方案 优秀 ...
- ASP.NET MVC3中Controller与View之间的数据传递总结
一. Controller向View传递数据 1. 使用ViewData传递数据 我们在Controller中定义如下: ViewData["Message_ViewData& ...
- orm映射 封装baseDao
是用orm映射封装自己封装dao层 思路:通过映射获得实体类的属性拼接sql语句 import java.lang.reflect.Field; import java.lang.reflect.In ...
- Hololens 手势事件执行顺序
InteractionManager_SourcePressed (Filename: C:\buildslave\unity\build\artifacts/generated/Metro/runt ...
- IIS最大连接数优化
错误信息:Error Summary:HTTP Error 503.2 - Service UnavailableThe serverRuntime@appConcurrentRequestLimit ...
- Java 数列求和
编写程序,显示1+2+3+4+5+6+7+8+9的结果 public class Homework006 { public static void main(String[] args) { int ...
- javascript 全局对象--w3school
JavaScript全局对象 1. decodeURI()解析某个编码的URI. 2.decodeURInComponent()解析一个编码的URI组件. 3.encodeURI()把字符串编码为U ...
- 关于 Poco::TCPServer框架 (windows 下使用的是 select模型) 学习笔记.
说明 为何要写这篇文章 ,之前看过阿二的梦想船的<Poco::TCPServer框架解析> http://www.cppblog.com/richbirdandy/archive/2010 ...
- 基于webdriver的jmeter性能测试-Eclipse+Selenium+JUnit生成jar包
续接 打开eclipse新建java项目,如下图所示: 输入项目名称后点击"完成"按钮,如下图所示: eclipse中新建一个java项目,如下图所示: 添加类库,如下图所示: 在 ...