Solved

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

Posted on 2008-10-07
5
363 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
  • 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 500 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IEnumerable<T> to a List<T> 8 51
Send form to asp server side 6 26
Adding 1 the value of input "text" every 3 seconds 5 32
VB script help 23 30
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 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