<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="CookieTest.aspx.vb" Inherits="OHNO.CookieTest" %>
<!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 Jim() {
var x = readCookieX('Jim');
alert(x);
document.getElementById('TextBox1').value = x; //อ่านค่าได้ไม่ถูกต้อง (หัวข้อที่ 3.)
}
//http://stackoverflow.com/questions/804584
function readCookieX(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(',');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0)
return (c.substring(nameEQ.length, c.length));
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text=".NET Read/Write Cookie Value" />
<asp:Button ID="Button2" runat="server" Text="JavaScript Read Cookie Value" OnClientClick="Jim();" />
<br />
</div>
<asp:Label ID="Label1" runat="server" Text="SubKey1"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="SubKey2"></asp:Label>
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server" Width="532px" Height="242px" TextMode="MultiLine"></asp:TextBox>
</form>
</body>
</html>
2. SourceCode Code (VB.NET)
Imports OHNO_Cookies
Imports System.Net.NetworkInformation
Public Class CookieTest
Inherits System.Web.UI.Page
Protected c As New OHNO_Cookies.myCookies("Jim")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
c.InsertValue("ThaiLang", "ทดสอบข้อความภาษาไทย")
c.InsertValue("LaoLang", "ຄົນທີ່ມີບຸນວາສນາ ຈະກະຕັນຍູພໍ່ແມ່ ")
End If
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim subKey1 As String = c.GetValue("ThaiLang")
Dim subKey2 As String = c.GetValue("LaoLang")
Label1.Text = subKey1 ' อ่านค่าได้ถูกต้อง
Label2.Text = subKey2 ' อ่านค่าได้ถูกต้อง
'๋JavaScript ในหน้า Markup อ่านค่าได้ไม่ถูกต้อง (อ่านไม่ออกนั่นแหละครับ)
End Sub
End Class
Imports System.Web
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Web.Security
Public Class myCookies
Private Shared _Current As HttpContext = HttpContext.Current
Private Shared _cookieName As String = "__myCookies__"
Private Shared _data As HybridDictionary = Nothing
Sub New()
End Sub
Sub New(ByVal cookieName As String)
_cookieName = cookieName
End Sub
Public Sub InsertValue(ByVal subKey As String, ByVal subValue As String)
If _Current.Response.Cookies(_cookieName) IsNot Nothing Then
Dim aCookie As HttpCookie = _Current.Response.Cookies(_cookieName)
aCookie.Values.Add(subKey, subValue)
Else
Dim aCookie As New HttpCookie(_cookieName)
aCookie.Values.Add(subKey, subValue)
_Current.Response.AppendCookie(aCookie)
End If
End Sub
Public Function GetValue(ByVal subKey As String) As String
Dim retValue As String = String.Empty
If _Current.Response.Cookies(_cookieName) IsNot Nothing Then
Dim aCookie As HttpCookie = _Current.Response.Cookies(_cookieName)
Try
retValue = aCookie.Values(subKey)
Catch ex As Exception
'Write error log.
End Try
End If
Return retValue
End Function
End Class