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.
'ASPX PAGE
' --------------------------------------------------------


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

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlYearSelect" runat="server">
            <asp:ListItem Value="2010"></asp:ListItem>
            <asp:ListItem Value="2011"></asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="btnGO" runat="server" Text="Go" />
        <br />
        <br />
        The cookie value is: 
        <asp:Label ID="lbYear" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>



' ASPX CODE BEHIND PAGE
' -------------------------------------------------------



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

ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
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

ASKER

Thanks