My cookie won't set. Why? :(

Discussion in 'ASP .Net' started by darrel, Sep 15, 2004.

  1. darrel

    darrel Guest

    I'm having some problems getting a cookie set on a button click. Here is my
    function:

    Private Sub buttonSaveSettings_Click(ByVal sender As System.Object, ByVal e
    As System.EventArgs) Handles buttonSaveSettings.Click
    'set the cookie for skipLinks
    dim skipLinksSetting as String =
    radioButtonListSkipLinks.SelectedItem.Value.tostring
    dim cookieMJBskipLinkSettings as new HttpCookie("MJBskipLinkSettings")
    if skipLinksSetting = "show" then
    cookieMJBskipLinkSettings.value = "show"
    else
    cookieMJBskipLinkSettings.value = "hide"
    end If
    cookieMJBskipLinkSettings.Expires = DateAdd(DateInterval.Year, 1, now)
    Response.Cookies.Add(cookieMJBskipLinkSettings)
    End Sub

    Anyone see any errors in the above logic/syntax?

    The problem seems to be that it always grabs the 'show' value from the
    options list. There are two options, one with a value of 'show' and one with
    a value of 'hide'. Even if I select 'hide' then trigger the button click, it
    appears to be set to 'show'. The asp control is set as such:

    <asp:RadioButtonList id=radioButtonListSkipLinks runat="server" >
    <asp:ListItem Value="show">Show Skip Links</asp:ListItem>
    <asp:ListItem Value="hide">Hide Skip Links</asp:ListItem>
    </asp:RadioButtonList>

    I'm completely stumped on this one. I really could use a second (third,
    fourth, etc.) set of eyes looking at this. ;o)

    -Darrel
     
    darrel, Sep 15, 2004
    #1
    1. Advertising

  2. darrel

    Marina Guest

    Are you sure you are not resetting the selected item to 'show' in page_load
    or something like that? Make sure your button click event is indeed placing
    'hide' into the cookie.

    "darrel" <> wrote in message
    news:...
    > I'm having some problems getting a cookie set on a button click. Here is

    my
    > function:
    >
    > Private Sub buttonSaveSettings_Click(ByVal sender As System.Object, ByVal

    e
    > As System.EventArgs) Handles buttonSaveSettings.Click
    > 'set the cookie for skipLinks
    > dim skipLinksSetting as String =
    > radioButtonListSkipLinks.SelectedItem.Value.tostring
    > dim cookieMJBskipLinkSettings as new HttpCookie("MJBskipLinkSettings")
    > if skipLinksSetting = "show" then
    > cookieMJBskipLinkSettings.value = "show"
    > else
    > cookieMJBskipLinkSettings.value = "hide"
    > end If
    > cookieMJBskipLinkSettings.Expires = DateAdd(DateInterval.Year, 1, now)
    > Response.Cookies.Add(cookieMJBskipLinkSettings)
    > End Sub
    >
    > Anyone see any errors in the above logic/syntax?
    >
    > The problem seems to be that it always grabs the 'show' value from the
    > options list. There are two options, one with a value of 'show' and one

    with
    > a value of 'hide'. Even if I select 'hide' then trigger the button click,

    it
    > appears to be set to 'show'. The asp control is set as such:
    >
    > <asp:RadioButtonList id=radioButtonListSkipLinks runat="server" >
    > <asp:ListItem Value="show">Show Skip Links</asp:ListItem>
    > <asp:ListItem Value="hide">Hide Skip Links</asp:ListItem>
    > </asp:RadioButtonList>
    >
    > I'm completely stumped on this one. I really could use a second (third,
    > fourth, etc.) set of eyes looking at this. ;o)
    >
    > -Darrel
    >
    >
    >
     
    Marina, Sep 15, 2004
    #2
    1. Advertising

  3. darrel

    darrel Guest

    > Are you sure you are not resetting the selected item to 'show' in
    page_load
    > or something like that?


    This is the only place I'm setting this particular cookie.

    > Make sure your button click event is indeed placing
    > 'hide' into the cookie.


    Well, I posted the event code in my post. Did you see anything odd about it?

    Thanks for taking a look at it!

    -Darrel
     
    darrel, Sep 15, 2004
    #3
  4. darrel

    Marina Guest

    I was talking about the radio buttons themselves being reset, not the
    cookie.

    If the cookie is being set at all, then clearly the cookie code is working.
    What I am saying, is that there is something about your radio button list,
    that perhaps when that code is run, 'show' is what is selected.

    "darrel" <> wrote in message
    news:...
    > > Are you sure you are not resetting the selected item to 'show' in

    > page_load
    > > or something like that?

    >
    > This is the only place I'm setting this particular cookie.
    >
    > > Make sure your button click event is indeed placing
    > > 'hide' into the cookie.

    >
    > Well, I posted the event code in my post. Did you see anything odd about

    it?
    >
    > Thanks for taking a look at it!
    >
    > -Darrel
    >
    >
     
    Marina, Sep 15, 2004
    #4
  5. darrel

    darrel Guest

    > I was talking about the radio buttons themselves being reset, not the
    > cookie.


    aha! Yes, that's the problem.

    on the page load, I read the cookie so that I can set the default selected
    items to match. Apparently, the button_click FIRST triggers the page load,
    which resets the selected items to the current cookie, and THEN sets the
    cookie...so that's just sending it in a loop.

    Is there a suggested workaround for this?

    I should note that I'm doing a postback THEN redirect. If I just go with the
    postback, obviously the page state will select the appropriate list item for
    me.

    The problem with the postback is that I'm setting a cookie that is read by
    another usercontrol on the same page. So, it looks like this:

    control1:readCookie
    control2:setCooke

    the problem is that the readCookie reads before the setCookie sets. So, on
    postback, it's READ -> SET requiring a page refresh to have control1 then
    read the new setting.

    Does that make sense? (I hope so)

    So, what I've been doing is using a redirect on the button-click handler.

    Whew...that was long winded. Let me restate this into one simpler question:

    1) is there a way that I can have the radial buttons default selection be
    based off of the cookie but have that happen AFTER the button-click sets it?

    -Darrel
     
    darrel, Sep 15, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,998
    Smokey Grindel
    Dec 2, 2006
  2. Replies:
    5
    Views:
    577
    Aaron Watters
    May 2, 2008
  3. Replies:
    2
    Views:
    474
  4. Phil Powell
    Replies:
    2
    Views:
    191
    Grant Wagner
    Sep 5, 2003
  5. Richard  Lawrence

    HTTP::Cookie won't store sent cookie

    Richard Lawrence, Mar 17, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    194
    Richard Lawrence
    Mar 21, 2005
Loading...

Share This Page