首页 技术 正文
技术 2022年11月16日
0 收藏 781 点赞 4,726 浏览 1268 个字

ASP.NET — WebForm —  Session的使用

Session是服务器端状态保持机制。

1. Test4.aspx文件与Test4.aspx.cs文件

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test4.aspx.cs" Inherits="Test4" %><!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="获取Session的值" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="设置Sessions" onclick="Button2_Click" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;public partial class Test4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
if (Session["UserName"]!=null)
{
Response.Write(Session["UserName"]);
}
else
{
Response.Write("Session中没有UserName的值");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Session["UserName"] = "TestUser";
}
}

2. 服务器在内存中存储Session数据时,服务器会开辟Session的存储区域,这个区域再分相应的存储单元,并且每个单元加上一个编号,这个编号叫SessionID。

  SessionID以cookie的形式返回给浏览器。Session默认的过期时间是20分钟。

ASP.NET — WebForm —  Session的使用

再次访问页面时,浏览器会将SessionID通过请求报文发送给服务器端。

ASP.NET — WebForm —  Session的使用

3. 由于Session数据是存储在服务器的内存中的,不要赋值太大的数据,防止占用内存过大。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731