ここではASP.NETのセッション変数の使い方について紹介します。

セッション変数とは

セッション変数は、セッションIDと紐づけてサーバー側に格納する変数です。セッション変数はブラウザが開いている間のみ情報を保持するための機能で、ブラウザを閉じるとセッション変数もクリアされます。

セッション変数を使った値の受け渡し

以下は WebForm1.aspx から WebForm2.aspx に値を受け渡すサンプルです。WebForm1.aspx の [次へ] ボタンをクリックしたらテキストボックスに入力された値をセッション変数として WebForm2.aspx に渡してラベルに表示します。

WebForm1.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>

<!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="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="次へ" />
        </div>
    </form>
</body>
</html>


VB.NET
WebForm1.aspx.vb

Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Session("text") = TextBox1.Text

        Response.Redirect("WebForm2.aspx")
    End Sub
End Class


C#
WebForm1.aspx.cs

using System;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Session["text"] = TextBox1.Text;

            Response.Redirect("WebForm2.aspx");
        }
    }
}


WebForm2.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm2.aspx.vb" Inherits="WebApplication1.WebForm2" %>

<!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:Label ID="Label1" runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>


VB.NET
WebForm1.aspx.vb

Public Class WebForm2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Session("text") Is Nothing Then
            Label1.Text = Session("text").ToString
        End If
    End Sub
End Class


C#
WebForm1.aspx.cs

using System;

namespace WebApplication1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["text"] != null)
            {
                Label1.Text = (string)Session["text"];
            }
        }
    }
}


セッション変数をクリアする

以下はセッション変数をクリアするサンプルです。

VB.NET

' セッション変数 "text" を削除する
Session.Remove("text")

' すべてのセッション変数を削除する
Session.RemoveAll()

' セッションそのものを削除する
Session.Abandon()


C#

// セッション変数 "text" を削除する
Session.Remove("text");

// すべてのセッション変数を削除する
Session.RemoveAll();

// セッションそのものを削除する
Session.Abandon();


セッションタイムアウト時間を設定する

ASP.NETのセッションタイムアウト時間はデフォルトで20分です。以下はセッションタイムアウト時間を設定するサンプルです。優先順位の順に紹介します。

ページのコードに設定する

VB.NET

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Session.Timeout = 30
End Sub


C#

protected void Page_Load(object sender, EventArgs e)
{
    Session.Timeout = 30;
}


Web.config に設定する

  <system.web>
	<sessionState mode="InProc" timeout="30" />
  </system.web>

mode属性の “InProc” はローカルのメモリ上に格納します。


以上、ASP.NETのセッション変数の使い方について解説しました。