Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > C# (.NET) > ASP.NET Object > (C#) ASP.NET Session Variables



Clound SSD Virtual Server

(C#) ASP.NET Session Variables

(C#) ASP.NET Session Variables() ใน ASP.NET เป็น Object ที่ใช้สำหรับการเก็บข้อมูลของ Client แต่ล่ะคนโดยแต่ล่ะ Client แต่ค่าตัวแปรทั้งหมดถูกจัดเก็บไว้ฝั่ง Web Server โดยจะมีหมายเลข Session ID ที่ไม่ซ้ำกัน เป็นตัวกำกับและคอยติดตามเก็บตัวแปรที่เกิดขึ้นภายในโปรแกรมภายใต้ตัวแปร Session ตัวแปร Session เป็นตัวแปรแบบ Global คือเมื่อมีการประกาศตัวแปรแล้ว จะสามารถเรียกใช้งานได้ในทุกส่วนของโปรแกรม และตัวแปร Session จะหายไปเมื่อมีการปิด Browser/ปิดเครื่อง Client หรือหมดอายุของ Session

Language Code : VB.NET || C#

Syntax

Session[.collection|property|method|event]


ค่าตัวแปรที่เกิดขึ้นภายใน Session จะเป็นตัวแปรที่ระบุ Client อย่างแน่นอน และจะไม่มีการเกิดข้อผิดพลาดและซ้ำซ้อนระหว่าง Client อื่น ๆ ซึ่งถ้าจะทำความเข้าใจง่าย ๆ คือ ค่าตัวแปรของแต่ล่ะ Client จะเป็นของใครของมัน ไม่เกี่ยวข้องกัน ซึ่งจะต่างกับตัวแปร Application ซึ่งจะเป็นการใช้ตัวแปรร่วมกันทั้งระบบ

การสร้างตัวแปร Session (Create Session)

Session["Session-Name"] = Value;
Session.Add("Session-Name","Value");



การอ่านตัวแปร Session (Read Session)

Session["Session-Name"];
Session.Item("Session-Name");



การลบตัวแปร Session (Delete Session)

Session.Abandon(); //*** Delete All ***//
Session.Clear(); //*** Delete All ***//
Session["Session-Name"] = null; //*** Delete Some Session ***//
Session.Remove("Session-Name"); //*** Delete Some Session ***//


Application Object



AspNetSession1.aspx

<%@ Page Language="C#" Debug="true" %>
<script runat="server">
    void Page_Load(Object sender , EventArgs e)
	{
		//*** Create Session ***//
		Session.Timeout  = 20;
		Session["SiteName"] = "www.ThaiCreate.Com";
		Session["Name"] = "Mr.Weerachai Nukitram";

		this.lblText.Text = "Session Created";

		this.hplLink.Text = "Click here to check";
		this.hplLink.NavigateUrl = "AspNetSession2.aspx";
    }

</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - Session Object</title>
</head>
<body>
    <form runat="server">
		<asp:Label id="lblText" runat="server"></asp:Label><br /><br />
        <asp:HyperLink id="hplLink" runat="server"></asp:HyperLink><br />
    </form>
</body>
</html>



AspNetSession2.aspx

<%@ Page Language="C#" Debug="true" %>
<script runat="server">
    void Page_Load(Object sender , EventArgs e)
	{
		//*** Read Session ***//
		Response.Write("Site Name = " + Session["SiteName"] + "<br>");
		Response.Write("Name = " + Session["Name"] +  "<br><br>");

		this.hplLink.Text = "Click here to delete";
		this.hplLink.NavigateUrl = "AspNetSession3.aspx";
    }

</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - Session Object</title>
</head>
<body>
    <form runat="server">
        <asp:HyperLink id="hplLink" runat="server"></asp:HyperLink><br />
    </form>
</body>
</html>









AspNetSession3.aspx

<%@ Page Language="C#" Debug="true" %>
<script runat="server">
    void Page_Load(Object sender , EventArgs e)
	{

		//Session.Abandon()
		Session["SiteName"] = null;
		Session["Name"] = null;
		
		this.lblText.Text = "Now Cookie Deleted";

		this.hplLink1.Text = "Click here to check";
		this.hplLink1.NavigateUrl = "AspNetSession2.aspx";

		this.hplLink2.Text = "Click here to create";
		this.hplLink2.NavigateUrl = "AspNetSession1.aspx";
    }

</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - Session Object</title>
</head>
<body>
    <form runat="server">
		<asp:Label id="lblText" runat="server"></asp:Label><br /><br />
        <asp:HyperLink id="hplLink1" runat="server"></asp:HyperLink><br />
        <asp:HyperLink id="hplLink2" runat="server"></asp:HyperLink><br />
    </form>
</body>
</html>


คำอธิบาย

AspNetSession1.aspx เป็นการสร้างตัวแปร Session (Create Session)
AspNetSession2.aspx เป็นการแสดงค่าตัวแปร Session (Read Session)
AspNetSession3.aspx เป็นการลบค่าตัวแปร Session (Delete Session)

Screenshot

ASP.NET Session Object

ASP.NET Session Object

ASP.NET Session Object









ASP.NET Session (DataSet & DataTable)

ASP.NET ArrayList & Session

ASP.NET HashTable & Session

ASP.NET SortedList & Session

ASP.NET StringDictionary & Session

   
Share

Property & Method (Others Related)

(C#) ASP.NET Session.SessionID() - Session Object
(C#) ASP.NET Session.Count() - Session Object
(C#) ASP.NET Session.Add() - Session Object
(C#) ASP.NET Session.Timeout() - Session Object
(C#) ASP.NET Session.Abandon() - Session Object
(C#) ASP.NET Session.Item() - Session Object
(C#) ASP.NET Session.Keys() - Session Object
(C#) ASP.NET Session.LCID() - Session Object
(C#) ASP.NET Session.Contents() - Session Object
(C#) ASP.NET Session.Contents.Remove() - Session Object
(C#) ASP.NET Session.Contents.RemoveAt() - Session Object
(C#) ASP.NET Session.Contents.RemoveAll() - Session Object
(C#) ASP.NET Session.StaticObjects() - Session Object
(C#) ASP.NET Session.CodePage() - Session Object
(C#) ASP.NET Session_Start() - Session Object
(C#) ASP.NET Session_End() - Session Object
(C#) ASP.NET Session & Object Value
(C#) ASP.NET Session & Array

ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2008-10-26 21:51:26 / 2017-03-28 22:05:51
  Download : Download  (C#) ASP.NET Session Variables
 Sponsored Links / Related

 
(C#) ASP.NET Response
Rating :

 
(C#) ASP.NET Request
Rating :

 
(C#) ASP.NET Request.Form
Rating :

 
(C#) ASP.NET Request.QueryString
Rating :

 
(C#) ASP.NET Cookies Variables
Rating :

 
(C#) ASP.NET Session Variables
Rating :

 
(C#) ASP.NET Global.asax
Rating :

 
(C#) ASP.NET Application Variables
Rating :

 
(C#) ASP.NET Server Variables
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่