คือต้องการเก็บค่าที่ได้จากการ เลือก dropdown ลง cookies ครับ แต่ค่าที่ได้มันผิด
เช่นถ้าเลือก TH ค่า cookies ที่ได้จะเป็น EN ถ้าเลือก EN ค่า cookies ที่ได้จะเป็น TH
ไม่แน่ใจว่าทำตรงไหนผิด ใครทราบรบกวนช่วยแนะนำด้วยครับ ขอบคุณครับ
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim testlangCookie As HttpCookie = New HttpCookie("testlangCookie")
If Not Page.IsPostBack Then
If IsNothing(Request.Cookies("testlangCookie")) Then
testlangCookie("testlangCookie") = "th-TH"
Response.Cookies.Add(testlangCookie)
testlangCookie.Expires = DateTime.Now.AddDays(1)
End If
Else
testlangCookie("testlangCookie") = ddlLanguages.SelectedValue
Response.Cookies.Add(testlangCookie)
testlangCookie.Expires = DateTime.Now.AddDays(1)
End If
If Request.Cookies("testlangCookie")("testlangCookie") Is Nothing Then
ddlLanguages.SelectedValue = "th-TH"
Else
ddlLanguages.SelectedValue = testlangCookie("testlangCookie")
End If
Response.Write("cookies value is " & Request.Cookies("testlangCookie")("testlangCookie"))
End Sub
Cookies มันจะต้อง Refresh ก่อน 1 ครั้งครับ ถึงจะอ่านค่าได้ครับ
Date :
2016-11-24 18:06:13
By :
mr.win
No. 2
Guest
วิธีแก้ไขทำเป็น Class (นิสัย) เก็บเอาไว้ (หลบหลีก Events PostBack/Not PostBack) Code (VB.NET)
Imports System.Web
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Web.Security 'Add Referance System.Web.Extentions
Public Class myCookies
Private Shared _Current As HttpContext = HttpContext.Current
Private Shared _cookieName As String = "__หน้าฮี__"
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
'Throw ex
End Try
End If
Return retValue
End Function
''' <summary>
''' Modify subKey and subValue
''' </summary>
''' <param name="subKey"></param>
''' <param name="subValue"></param>
''' <returns>True/False</returns>
''' <remarks>If subKey not exists recrete new</remarks>
Public Function ModifyValue(ByVal subKey As String, ByVal subValue As String) As Boolean
Dim suc As Boolean = False
If _Current.Request.Cookies(_cookieName) IsNot Nothing Then
Try
_Current.Request.Cookies(_cookieName)(subKey) = subValue
suc = True
Catch ex As Exception
Try
InsertValue(subKey, subValue)
suc = True
Catch ex1 As Exception
Throw ex1
End Try
End Try
End If
Return suc
End Function
Public Sub Save()
' Setting a cookie's value and/or subvalue using the HttpCookie class
Dim cookie As HttpCookie
If _Current.Request.Cookies(_cookieName) IsNot Nothing Then
_Current.Request.Cookies.Remove(_cookieName)
End If
cookie = New HttpCookie(_cookieName)
If _data.Count > 0 Then
Dim cookieData As IEnumerator = _data.GetEnumerator()
Dim item As DictionaryEntry
While cookieData.MoveNext()
item = CType(cookieData.Current, DictionaryEntry)
cookie.Values.Add(item.Key.ToString(), item.Value.ToString())
End While
End If
_Current.Response.AppendCookie(cookie)
End Sub
Public Sub Delete()
' Set the value of the cookie to null and set its expiration to some time in the past
If _Current.Response.Cookies(_cookieName) IsNot Nothing Then
_Current.Response.Cookies(_cookieName).Value = Nothing
' last month
_Current.Response.Cookies(_cookieName).Expires = System.DateTime.Now.AddMonths(-1)
End If
End Sub
Public Sub Delete(ByVal subKey As String)
' Set the value of the cookie to null and set its expiration to some time in the past
If _Current.Response.Cookies(_cookieName) IsNot Nothing Then
Dim aCookie As HttpCookie = _Current.Request.Cookies(_cookieName)
aCookie.Values.Remove(subKey)
aCookie.Expires = DateTime.Now.AddDays(-1)
_Current.Response.Cookies.Add(aCookie)
End If
End Sub
''' <summary>
''' Reading Cookie Collections
''' </summary>
''' <remarks></remarks>
Public Function ReadingCookieCollections() As String
Dim output As System.Text.StringBuilder = New System.Text.StringBuilder()
Dim aCookie As HttpCookie
Dim subkeyName, subkeyValue As String
For i As Integer = 0 To _Current.Request.Cookies.Count - 1
aCookie = _Current.Request.Cookies(i)
output.Append("Name = " & aCookie.Name & "<br />")
If aCookie.HasKeys Then
Dim CookieValues As System.Collections.Specialized.NameValueCollection = aCookie.Values
Dim CookieValueNames() As String = CookieValues.AllKeys
For j As Integer = 0 To CookieValues.Count - 1
subkeyName = _Current.Server.HtmlEncode(CookieValueNames(j))
subkeyValue = _Current.Server.HtmlEncode(CookieValues(j))
output.Append("Subkey name = " & subkeyName & "<br />")
output.Append("Subkey value = " & subkeyValue & "<br /><br />")
Next
Else
output.Append("Value = " & _Current.Server.HtmlEncode(aCookie.Value) & "<br /><br />")
End If
Next
Return output.ToString()
End Function
End Class
Private Function findDMY(ByVal txt As String, Optional ByVal dmy As String = "m") As String
Dim strMonthRet As String = ""
For i As Integer = 0 To txt.Length - 1
If (_Mask(i).ToString()).ToLower() = dmy.ToLower() Then
strMonthRet &= txt.Substring(i, 1)
End If
Next
strMonthRet = If(strMonthRet = "", "0", strMonthRet) 'CInt(If(strMonthRet = "", "0", strMonthRet)) '.ToString("D2") 'Padding zero 01, 02, ..., 12
Return strMonthRet '1-31, 01-31, 1-12, 01-12
End Function
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
If AscW(e.KeyChar) = Keys.Back OrElse AscW(e.KeyChar) = Keys.Delete OrElse AscW(e.KeyChar) = Keys.Left OrElse AscW(e.KeyChar) = Keys.Right Then
e.Handled = False
Return
End If
If Mask <> "" Then
e.Handled = True
Dim newText As String = Me.Text
Dim Finished As Boolean = False
Dim lstMaxDays As String() = {"31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"}
For i As Integer = Me.SelectionStart To _Mask.Length - 1
Select Case (_Mask(i).ToString()).ToLower()
Case "d"
If (Char.IsDigit(e.KeyChar)) Then
newText &= e.KeyChar.ToString()
Dim getDay As String = findDMY(newText, "d")
If (getDay.Length = 0 AndAlso e.KeyChar > "3") OrElse (getDay.Length = 2 AndAlso newText = "3" AndAlso e.KeyChar > "1") Then
Return
ElseIf CInt(getDay) > 31 OrElse getDay = "00" Then
Return
End If
'If CInt(getDay) > 31 OrElse getDay = "00" Then
' Return
'End If
Finished = True
Else
Return
End If
Case "m"
If (Char.IsDigit(e.KeyChar)) Then
newText &= e.KeyChar.ToString()
Dim getMonth As String = findDMY(newText, "m")
If CInt(getMonth) > 12 OrElse getMonth = "00" Then
Return
End If
Finished = True
Else
Return
End If
Case "y"
If (Char.IsDigit(e.KeyChar)) Then
newText &= e.KeyChar.ToString()
Finished = True
Else
Return
End If
Case Else 'Mask symbol.
newText &= _Mask(i)
End Select
If Finished Then
Exit For
End If
Next
'Update the Text.
Me.Text = newText
Me.SelectionStart = Me.Text.Length
End If
End Sub
Imports System.Drawing
Imports System.Windows.Forms
'
<ToolboxBitmap(GetType(TextBox))> _
Public Class usrCtrlMaskTextBox : Inherits TextBox
#Region " Private Variables "
Private WithEvents _DTP As New DateTimePicker 'เอาไว้กันเหนียว (เรื่องอะไรตูจะไปคำนวณด้วยตัวเอง +55555)
Private _Mask As String 'กำหนดได้ตามใจชอบเช่น "ไก่งามเพราะขนคนงามเพราะหอย"
Private _Button As New Button() With {.Text = String.Empty} 'ปุ่มแสดงวันที่ให้เลือก
#End Region
#Region " Properties "
<System.ComponentModel.DefaultValue(GetType(String), "Text"), System.ComponentModel.Category("Appearance")> _
Public Property Mask() As String
Get
Return _Mask 'เช่น หำMM/dd/yyyy, yyyy/หำdd/MM, etc... รองรับทุกรูปแบบ
End Get
Set(ByVal value As String)
_Mask = value
End Set
End Property
Private mKey As String
<System.ComponentModel.DefaultValue(GetType(String), "Text"), System.ComponentModel.Category("Appearance")> _
Public Property _Key() As String
Get
Return mKey
End Get
Set(ByVal value As String)
mKey = value
End Set
End Property
#End Region
#Region " Constructor "
Public Sub New()
AddHandler _Button.Click, Sub(หอย, งาม)
'
'Display Date Dialog.
'
End Sub
End Sub
#End Region
Protected Overrides Sub OnKeyDown(e As KeyEventArgs)
'MyBase.OnKeyDown(e)
'e.Handled = True
End Sub
Private Function findDMY(ByVal txt As String, Optional ByVal dmy As String = "m") As String
Dim strRet As String = ""
For i As Integer = 0 To txt.Length - 1
If (_Mask(i).ToString()).ToLower() = dmy.ToLower() Then
strRet &= txt.Substring(i, 1)
End If
Next
Return If(strRet = "", "0", strRet)
End Function
''' <summary>
''' ตรวจสอบเดือนกุมภาพันธ์มี 29 วันหรือไม่?
''' Dim isLeap As Boolean = isLeapyear(2000)
''' </summary>
''' <param name="shrYear"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function isLeapyear(ByVal shrYear As Short) As Boolean
Return Date.IsLeapYear(shrYear)
End Function
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
If AscW(e.KeyChar) = Keys.Back OrElse AscW(e.KeyChar) = Keys.Delete OrElse AscW(e.KeyChar) = Keys.Left OrElse AscW(e.KeyChar) = Keys.Right Then
e.Handled = False
Return
End If
If Mask <> String.Empty Then
e.Handled = True
Dim newText As String = Me.Text
Dim Finished As Boolean = False
Dim daysInMonth = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
Dim mask1 As String = String.Empty
For i As Integer = Me.SelectionStart To _Mask.Length - 1
mask1 = _Mask(i).ToString().ToLower()
If Not (Char.IsDigit(e.KeyChar)) Then
Return
Else
If "dmy".Contains(mask1) Then
newText &= e.KeyChar.ToString()
Finished = True
Else 'Mark symbol
newText &= mask1
Finished = False
End If
End If
Select Case mask1
Case "d"
Dim getDay As String = findDMY(newText, "d")
'ป้องกันการป้อนวันที่ >= 31 และ 00
If (CByte(getDay) > 31) OrElse (getDay.Length = 1 AndAlso e.KeyChar > "3") OrElse (getDay.Length > 1 AndAlso getDay.Substring(0, 2) = "00") Then
Return
End If
Case "m"
Dim getDay As String = findDMY(newText, "d")
Dim getMonth As String = findDMY(newText, "m")
'ป้องกันการป้อนเดือน > 12 และป้อนเดือน 00
If CByte(getMonth) > 12 OrElse (getMonth.Length > 1 AndAlso getMonth.Substring(0, 2) = "00") Then
Return
End If
'ป้อนเดือน 01 - 09 หรือ 1 - 9
If CByte(getMonth) > 0 Then
Dim lastDayOfMonth As Byte = daysInMonth(getMonth - 1)
If CByte(getDay) > lastDayOfMonth Then
Return
End If
If getMonth.Length = 1 AndAlso CByte(getMonth) <> 1 Then
getMonth = "0" & getMonth
'ตั้งใจทำ พิมพ์ 2 --> 02, 3 --> 03, 9 -->09 เป็นต้น แต่ขี้เกียจทำแล้ว
End If
ElseIf getMonth.Length > 1 AndAlso getMonth.Substring(0, 2) = "00" Then
Return
End If
Case "y"
Dim getDay As String = findDMY(newText, "d")
Dim getMonth As String = findDMY(newText, "m")
Dim getYear As String = findDMY(newText, "y")
If Not isLeapyear(CShort(getYear)) Then
daysInMonth(1) = 28 'เดือนกุมภาพันธ์มี่ 28 วัน
End If
'
'ตรวสอบปีที่ป้อน เดือนกุมพาพันธ์มี 29 วันหรือไม่?
'ขี้เกียจทำแล้วว่ะเฮ้ย
'
End Select
If Finished Then
Exit For
End If
Next
'Update the Text.
Me.Text = newText
Me.SelectionStart = Me.Text.Length
End If
End Sub
Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
'ยังไม่ได้ทำและขี้เกียจทำ
End Sub
Public Sub ResetValue()
MyBase.Text = String.Empty
End Sub
Protected Overrides Sub OnCausesValidationChanged(e As EventArgs)
MyBase.OnCausesValidationChanged(e)
End Sub
End Class
Date :
2016-11-25 15:30:22
By :
หน้าฮี
No. 8
Guest
จาก #NO7 บรรทัดที่ 57 ต้องตรวจสอบเพิ่มเติมดังนี้
Code (VB.NET)
If Array.IndexOf(_Mask, i) <> -1 Then
If (_Mask(i).ToString()).ToLower() = dmy.ToLower() Then
strRet &= txt.Substring(i, 1)
End If
End If