My cookie won't set. Why? :(

  • Thread starter Thread starter darrel
  • Start date Start date
D

darrel

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
 
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.
 
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
 
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.
 
I was talking about the radio buttons themselves being reset, not the

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
 
Back
Top