set cookie using javascript

I see my application using javascript to update the cookie value:

            
function ActDocTabChanged() {
		    var ActDocTabText = TabStripActiveDocuments.getSelectedTab().get_index();

		    if (ActDocTabText == 1)
		    {
		        $.cookie('ActiveDocsClicked', "false");
		    }
		    if (ActDocTabText == 0)
		    {
		        $.cookie('ActiveDocsClicked', "true");
		    }
		}

Open in new window


But I want to update cookie value from my C# program:

         
  if (currentForm.FormStateID == 1)
            {
                Response.Write("<script language=javascript>");
                Response.Write("$.cookie('ActiveDocsClicked', 'false');");
                Response.Write("alert($.cookie('ActiveDocsClicked'));");
                Response.Write("</script>");                  
            }

Open in new window


But I don't know why the cookie value is not updated. Thank you!!
jssong2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
http://msdn.microsoft.com/en-us/library/aa287547(v=vs.71).aspx

HttpCookie myCookie = new HttpCookie("ActiveDocsClicked");
DateTime now = DateTime.Now;
// Set the cookie value.
myCookie.Value = "false";
// Set the cookie expiration date.
myCookie.Expires = now.AddYears(10); // expire in 10 years from now
// Add the cookie.
Response.Cookies.Add(myCookie);

Open in new window

0
jssong2000Author Commented:
Iam sorry. I could not change cookie value once I created.
Is there any setup on the machine? Thanks.
0
leakim971PluritechnicianCommented:
until you disable cookies on your browser settings, no, nothing to do

but you need to understand that if you set cookie with your server (c#) you need to do that before << sending >> the page to allow the browser to access it
each time your browser request a page to your web server it get back first an header, this header embed the cookies which allow the browser to access it.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

jssong2000Author Commented:
Anyway once I created and set up a value. I could not change its value again.
I try to set it expire first and then create again.


                DateTime now = DateTime.Now;
                Response.Cookies["ActiveDocsClicked"].Expires = now.AddMinutes(-1);
                HttpCookie myCookie = new HttpCookie("ActiveDocsClicked");
               

                // Set the cookie value.
                myCookie.Value = "false";
                // Set the cookie expiration date.
                myCookie.Expires = now.AddMinutes(30);

                // Add the cookie.
                Response.Cookies.Add(myCookie);
      string ReqAct = Request.Cookies["ActiveDocsClicked"].Value;
0
leakim971PluritechnicianCommented:
where how when are you trying to overwrite it?
0
jssong2000Author Commented:
The previous code is in a function. Everytime I try to make it expire:

Response.Cookies["ActiveDocsClicked"].Expires = now.AddMinutes(-1);

And then create and set value again.



        if (Forms.Contains(currentForm))
        {
            if (currentForm.FormStateID == 1)
            {
                //required active
                DateTime now = DateTime.Now;
                Response.Cookies["ActiveDocsClicked"].Expires = now.AddMinutes(-1);
                HttpCookie myCookie = new HttpCookie("ActiveDocsClicked");
               

                // Set the cookie value.
                myCookie.Value = "false";
                // Set the cookie expiration date.
                myCookie.Expires = now.AddMinutes(30);

                // Add the cookie.
                Response.Cookies.Add(myCookie);

                string ReqAct = Request.Cookies["ActiveDocsClicked"].Value;
                 
            }
            else if (currentForm.FormStateID == 2)
            {
                //required historic
                DateTime now = DateTime.Now;
                Response.Cookies["HistoricDocsClicked"].Expires = now.AddMinutes(-1);

                HttpCookie myCookie2 = new HttpCookie("HistoricDocsClicked");
               
                // Set the cookie value.
                myCookie2.Value = "false";
                // Set the cookie expiration date.
                myCookie2.Expires = now.AddMinutes(30);

                // Add the cookie.
                Response.Cookies.Add(myCookie2);
                string ReqHis = Request.Cookies["HistoricDocsClicked"].Value;

            }
        }
        else
        {
            if (currentForm.FormStateID == 1)
            {
                //non-required active
                DateTime now = DateTime.Now;
                Response.Cookies["ActiveDocsClicked"].Expires = now.AddMinutes(-1);
                HttpCookie myCookie = new HttpCookie("ActiveDocsClicked");
               

                // Set the cookie value.
                myCookie.Value = "true";
                // Set the cookie expiration date.
                myCookie.Expires = now.AddMinutes(30);

                // Add the cookie.
                Response.Cookies.Add(myCookie);

                string NonReqAct = Request.Cookies["ActiveDocsClicked"].Value;

            }
            else if (currentForm.FormStateID == 2)
            {
                //non-required historic
                DateTime now = DateTime.Now;
                Response.Cookies["HistoricDocsClicked"].Expires = now.AddMinutes(-1); ;
                HttpCookie myCookie2 = new HttpCookie("HistoricDocsClicked");
               

                // Set the cookie value.
                myCookie2.Value = "true";
                // Set the cookie expiration date.
                myCookie2.Expires = now.AddMinutes(30);

                // Add the cookie.
                Response.Cookies.Add(myCookie2);
                string ReqHis = Request.Cookies["HistoricDocsClicked"].Value;

            }
        }
0
jssong2000Author Commented:
The function is an event:

 protected void gvReviewEmployeeDocuments_SelectedIndexChanged(object sender, System.EventArgs e)

{
    ....
           if (Forms.Contains(currentForm))
        {
            if (currentForm.FormStateID == 1)
            {
                //required active
                DateTime now = DateTime.Now;
                Response.Cookies["ActiveDocsClicked"].Expires = now.AddMinutes(-1);
                HttpCookie myCookie = new HttpCookie("ActiveDocsClicked");
               

                // Set the cookie value.
                myCookie.Value = "false";
                // Set the cookie expiration date.
                myCookie.Expires = now.AddMinutes(30);

                // Add the cookie.
                Response.Cookies.Add(myCookie);

                string ReqAct = Request.Cookies["ActiveDocsClicked"].Value;
                 
            }
            else if (currentForm.FormStateID == 2)
            {
                //required historic
                DateTime now = DateTime.Now;
                Response.Cookies["HistoricDocsClicked"].Expires = now.AddMinutes(-1);

                HttpCookie myCookie2 = new HttpCookie("HistoricDocsClicked");
               
                // Set the cookie value.
                myCookie2.Value = "false";
                // Set the cookie expiration date.
                myCookie2.Expires = now.AddMinutes(30);

                // Add the cookie.
                Response.Cookies.Add(myCookie2);
                string ReqHis = Request.Cookies["HistoricDocsClicked"].Value;

            }
        }
        else
        {
            if (currentForm.FormStateID == 1)
            {
                //non-required active
                DateTime now = DateTime.Now;
                Response.Cookies["ActiveDocsClicked"].Expires = now.AddMinutes(-1);
                HttpCookie myCookie = new HttpCookie("ActiveDocsClicked");
               

                // Set the cookie value.
                myCookie.Value = "true";
                // Set the cookie expiration date.
                myCookie.Expires = now.AddMinutes(30);

                // Add the cookie.
                Response.Cookies.Add(myCookie);

                string NonReqAct = Request.Cookies["ActiveDocsClicked"].Value;

            }
            else if (currentForm.FormStateID == 2)
            {
                //non-required historic
                DateTime now = DateTime.Now;
                Response.Cookies["HistoricDocsClicked"].Expires = now.AddMinutes(-1); ;
                HttpCookie myCookie2 = new HttpCookie("HistoricDocsClicked");
               

                // Set the cookie value.
                myCookie2.Value = "true";
                // Set the cookie expiration date.
                myCookie2.Expires = now.AddMinutes(30);

                // Add the cookie.
                Response.Cookies.Add(myCookie2);
                string ReqHis = Request.Cookies["HistoricDocsClicked"].Value;

            }
        }
}
0
leakim971PluritechnicianCommented:
do you've a postback or reloading the page between two updates?
0
jssong2000Author Commented:
Make it simple. I created new website with default.aspx and secondPage.aspx.

On the first page, I created a button. And there is a button click event.

    protected void Button1_Click(object sender, EventArgs e)
    {
        int strTest = 1;

        if (strTest == 1)
        {
            Response.Cookies["test1"].Value = "false";
        }
        else
        {
            Response.Cookies["test1"].Value = "true";
        }

        string testReq = Request.Cookies["test1"].Value;
        Response.Redirect("SecondPage.aspx");
    }


The purpose is to set the cookie value to false. And redirect to second page.

On Page_Load of second page:

    protected void Page_Load(object sender, EventArgs e)
    {
        string testReq = Request.Cookies["test1"].Value;
       
        HttpCookie myCookie3 = new HttpCookie("test1", "true");
       
        Response.SetCookie(myCookie3);
        myCookie3.Expires = DateTime.Now.AddYears(10);
        //Response.Cookies[0].Value = myCookie3.Value;
        //Response.Cookies["test1"].Value = "true";
        testReq = Request.Cookies["test1"].Value;
        string testReq2 = Request.Cookies[0].Value;
        string testReq3 = Request.Cookies[1].Value;
    }

The purpose of pageload is to set cookie value to true.
I want to overwrite cookie value to true.
But It didn't. Instead It create two index about test1 cookie. index 0 's value is false and index 1 's value true. But Request.Cookies["test1"].Value is equal to "false" whick is not working for me.

Thanks!!
0
leakim971PluritechnicianCommented:
You've the
Request object which come from the browser (or the previous page if you're using a redirect)

and the
Response object which is the answer to the request and which is sent to the browser (or the next page if you're using a redirect)


protected void Page_Load(object sender, EventArgs e)
    {
        string testReq = Request.Cookies["test1"].Value;
       
        HttpCookie myCookie3 = new HttpCookie("test1", "true"); // send this to the browser
       
        Response.SetCookie(myCookie3);

        testReq = Request.Cookies["test1"].Value;  // YOU STILL READ THE OLD COOKIE because it's the Request object which COME FROM the browser, use Response.Cookies["test1"].Value; to read the value the browser(or next page) are GOING TO receive
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jssong2000Author Commented:
Thank you!! It worked.
0
jssong2000Author Commented:
Thank you!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.