Tips and tricks for .NET using ASP and VB code.

Initialize values from cookies

Partial Class _Default
    Inherits System.Web.UI.Page

    Function GetCookie(ByVal Sender As Control, ByVal PropertyName As String) As String
        Dim ReturnValue As String = Nothing
        Dim ControlId As String = Sender.ID
        Try
            If Request.Cookies(ControlId) IsNot Nothing Then
                ReturnValue = Server.HtmlDecode(Request.Cookies(ControlId)(PropertyName)).ToString
            End If
        Catch
        End Try
        Return ReturnValue
    End Function

    Protected Sub SetCookie(ByVal Sender As Control, ByVal PropertyName As String, ByVal CookieValue As String)
        Dim ThisCookie As New HttpCookie(Sender.ID)
        ThisCookie.Expires = Now.AddYears(1)
        ThisCookie.Values(PropertyName) = Server.HtmlEncode(CookieValue)
        Try
            Response.Cookies.Add(ThisCookie)
        Catch
        End Try
    End Sub

    Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        SetCookie(CType(sender, Control), "Checked", CheckBox1.Checked.ToString)
    End Sub

    Protected Sub CheckBox1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.PreRender
        If IsPostBack = False Then
            If GetCookie(CType(sender, Control), "Checked") IsNot Nothing Then
                CheckBox1.Checked = CBool(GetCookie(CType(sender, Control), "Checked"))
                Dim temp1 As Boolean = CBool("True")
                Dim temp2 As Boolean = CBool("False")
                Dim temp3 As String = GetCookie(CType(sender, Control), "Checked")
            End If
        End If
    End Sub

    Protected Sub CheckBoxList1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.PreRender
        If IsPostBack = False Then
            If GetCookie(CType(sender, Control), "SelectedValues") IsNot Nothing Then
                Dim SelectedValuesJoined As String = GetCookie(CType(sender, Control), "SelectedValues")
                Dim SelectedValuesArray() As String = Split(SelectedValuesJoined, ",")
                For Each CheckBoxListItem As ListItem In CheckBoxList1.Items
                    If SelectedValuesArray.Contains(CheckBoxListItem.Value) Then
                        CheckBoxListItem.Selected = True
                    Else
                        CheckBoxListItem.Selected = False
                    End If
                Next CheckBoxListItem
            End If
        End If
    End Sub

    Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged
        Dim SelectedValuesList As New Generic.List(Of String)
        For Each CheckBoxListItem As ListItem In CheckBoxList1.Items
            If CheckBoxListItem.Selected = True Then
                SelectedValuesList.Add(CheckBoxListItem.Value)
            End If
        Next CheckBoxListItem
        Dim SelectedValuesJoined As String = String.Join(",", SelectedValuesList.ToArray)
        SetCookie(CType(sender, Control), "SelectedValues", SelectedValuesJoined)
    End Sub

    Protected Sub TextBox1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.PreRender
        If IsPostBack = False Then
            If GetCookie(CType(sender, Control), "Text") IsNot Nothing Then
                'Receiving text from a cookie can be hazardous.
                TextBox1.Text = GetCookie(CType(sender, Control), "Text")
            End If
        End If
    End Sub

    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        SetCookie(CType(sender, Control), "Text", TextBox1.Text)
    End Sub

    Protected Sub RadioButton1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.PreRender
        If IsPostBack = False Then
            If GetCookie(CType(sender, Control), "Checked") IsNot Nothing Then
                RadioButton1.Checked = CBool(GetCookie(CType(sender, Control), "Checked"))
            End If
        End If
    End Sub

    Protected Sub RadioButton1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        SetCookie(CType(sender, Control), "Checked", RadioButton1.Checked.ToString)
    End Sub

No comments:

Post a Comment

Search This Blog