Link to home
Start Free TrialLog in
Avatar of cdemott33
cdemott33Flag for United States of America

asked on

Help with setting/modify a cookie value

I have a page that passes a year value via the query string.  On page load I need to grab that value and store it in a cookie.  I'm able to do that just fine but this issue has do with with resetting my cookies value.

On my page I have a drop down that allows my users to selected a new date.  When the user clicks the GO button I want to reset the value of my cookie to whatever the selected value of the drop down is.  

My problem is this.

You have to hit the GO button TWICE in order for the value of the drop down to be assigned to the cookie.  Could someone look at my code and tell me what I'm doing wrong.
' --------------------------------------------------------

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Cookies.aspx.vb" Inherits="Cookies" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:DropDownList ID="ddlYearSelect" runat="server">
            <asp:ListItem Value="2010"></asp:ListItem>
            <asp:ListItem Value="2011"></asp:ListItem>
        <asp:Button ID="btnGO" runat="server" Text="Go" />
        <br />
        <br />
        The cookie value is: 
        <asp:Label ID="lbYear" runat="server"></asp:Label>

' -------------------------------------------------------

Partial Class Cookies
    Inherits System.Web.UI.Page

    Dim strYearCal As String

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

        Call CreateCookie()

        lbYear.Text = Server.HtmlEncode(Request.Cookies("calyear").Value)

    End Sub

    Sub CreateCookie()

        strYearCal = Request.QueryString("calyear")

        If Request.Cookies("calyear") Is Nothing Then
            Response.Cookies("calyear").Value = Request.QueryString("calyear")
        End If

    End Sub

    Protected Sub btnGO_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGO.Click
        Response.Cookies("calyear").Value = ddlYearSelect.SelectedValue
    End Sub

End Class

Open in new window

Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cdemott33