Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Setting a cookie in ASP.net based on a drop down list

Posted on 2008-10-07
5
Medium Priority
?
369 Views
Last Modified: 2010-04-21
I'm taking my first attempt at setting a cookie in ASP.net.  I have a drop down list that lists all users of the application.  Once the user has made their selection from the drop down list, they have the option of clicking a checkbox.  When they click the checkbox, I would like for the value of the dropdownlist to be saved as a cookie.  I would like to use that value to set the default value of the drop down list.  I'm not sure what kind of event to put my code in or even if the code is correct.  I've pasted it below.  Thank you for your help.
Dim currentofficer As DropDownList = DirectCast(FindControl("DropDownOfficer"), DropDownList)
        If currentofficer.SelectedValue <> "All Officers" Then
            Response.Cookies("PipelineCookie")("DefaultOfficer") = currentofficer.Text
        Else
        End If

Open in new window

0
Comment
Question by:ryanvb83
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:crazyman
ID: 22659692

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim s As String = Response.Cookies("PipelineCookie")("DefaultOfficer")
            If Not String.IsNullOrEmpty(s) Then
                DropDownList1.SelectedValue = s
            End If
 
        End If
    End Sub
 
    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
 
        Response.Cookies("PipelineCookie")("DefaultOfficer") = DropDownList1.SelectedValue
    End Sub

Open in new window

0
 
LVL 13

Accepted Solution

by:
crazyman earned 2000 total points
ID: 22659740
sorry my vb sucks, try this.
you could set autopostback = true on your dropdown
you may also want to control the cookie expiration a little better...
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim cook As HttpCookie = Request.Cookies("PipelineCookie")
            If Not cook Is Nothing Then
                Dim s As String = cook("DefaultOfficer")
                If Not String.IsNullOrEmpty(s) Then
                    DropDownList1.SelectedValue = s
                End If
            End If
            
 
        End If
    End Sub
 
    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
 
        Response.Cookies("PipelineCookie")("DefaultOfficer") = DropDownList1.SelectedValue
    End Sub

Open in new window

0
 

Author Comment

by:ryanvb83
ID: 22659933
Awesome.  That worked great after I turned on AutoPostBack for the checkbox control.  How do I set the cookie to never expire?
0
 
LVL 13

Assisted Solution

by:crazyman
crazyman earned 2000 total points
ID: 22659965
something like...

Response.Cookies("PipelineCookie")("DefaultOfficer").Expires = DateTime.Now.AddYears(50)
0
 

Author Closing Comment

by:ryanvb83
ID: 31503825
Perfect.  Thanks!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question