473,320 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

My cookie won't set. Why? :(

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.tostri ng
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

Nov 18 '05 #1
4 1756
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" <no*****@hotmail.com> wrote in message
news:uq**************@TK2MSFTNGP11.phx.gbl...
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.tostri ng
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

Nov 18 '05 #2
> 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
Nov 18 '05 #3
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" <no*****@hotmail.com> wrote in message
news:eW*************@tk2msftngp13.phx.gbl...
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

Nov 18 '05 #4
> 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
Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael | last post by:
I am reading and setting a cookie using JavaScript in the BODY onload and onunload events respectively. This works fine. However when I use ASP to set the cookie under some condition where I...
8
by: John Dalberg | last post by:
What happens when a cookie expires? Does it mean that when the browser or sessions ends, it doesn't get saved? I am using Opera and looking at available cookies and I can some cookies that have...
3
by: Ben Amada | last post by:
Every time a user visits a particular page, I'm using the code below to add a cookie that expires in 1 day. My question is, a user may visit this page a few times within one day. Before adding...
8
by: bashful.belle | last post by:
I'm using Forms authentication and a non persistent cookie in my asp.net application. How do i get the cookie to time out after a period of inactivity, say 10 minutes, and force the user to login...
1
by: Stef | last post by:
Hi people, I have a problem with cookies set via javascript. What I try to achieve is, when a user comes on the intranet, he can click on a link ( a simple href) that will set the content to...
6
by: kelvlam | last post by:
Hello all, I'm still a bit new with JavaScript, and I hope the guru here can shed some light for me. It's regarding handling cookie and the case-sensitive nature of JavaScript itself. My...
7
by: monomaniac21 | last post by:
hi i have a php site which allows users to save a cookie on their computer which stores their user id details and allows them to auto- login. i'm wondering whether this is safe, is it...
7
by: =?Utf-8?B?SlA=?= | last post by:
I need to design a WS that will after authenicating the user, create a cookie on the users PC that made the request. All the code I keep finding is how to get a WS to read a cookie, I need it to...
5
by: Mike Copeland | last post by:
In a Web site I'm building, I want to provide an ability to set a "flag" message that users can see when they log on. Specifically, I want the "flag" to show when the (snail) mail for our building...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.