My cookie won't set. Why? :(

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
 
M

Marina

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.
 
D

darrel

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
 
M

Marina

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.
 
D

darrel

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top