Link to home
Start Free TrialLog in
Avatar of Cerixus
CerixusFlag for United States of America

asked on

ASP.NET Overwrite a cookie

I'm using the attached javascript code to set a cookie from view.aspx.  What I would like to do, is overwrite that cookie from default.aspx.  I've tried the following four methods and none of them seem to overwrite what was set with the js function.  All of these methods were in the Page_Load.

Method 1:
        Dim aCookie As New HttpCookie("ActiveTab")
        aCookie.Value = "1"
        aCookie.Expires = DateTime.Now.AddDays(30)
        Response.Cookies.Add(aCookie)

Method 2:
         Response.Cookies.Add(New HttpCookie("ActiveTab", "1"))

Method 3:
         Response.Cookies("ActiveTab").Value = "1"
         Response.Cookies("ActiveTab").Expires = DateTime.Now.AddDays(30)

Method 4: (Just expire the cookie)
        If (Not Request.Cookies("ActiveTab") Is Nothing) Then
            Dim myCookie As HttpCookie
            myCookie = New HttpCookie("ActiveTab")
            myCookie.Expires = DateTime.Now.AddDays(-1D)
            Response.Cookies.Add(myCookie)
        End If
function clientActiveTabChanged(sender, args) {
      var ActiveTab = sender.get_activeTabIndex() ;
          var expiredays =30;
          var c_name="ActiveTab";
          var exdate=new Date();
      exdate.setDate(exdate.getDate()+expiredays);
      document.cookie=c_name+ "=" +escape(ActiveTab)+
      ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
      }

Open in new window

Avatar of Cerixus
Cerixus
Flag of United States of America image

ASKER

In the page_load of the view page ....

If(!IsPostBack)
{
TabPanel1.ActiveTabIndex = 1;
}

just see if it works .. other wise we ll go for cookie overwrite
Avatar of Cerixus

ASKER

No, that won't work.
ASKER CERTIFIED SOLUTION
Avatar of masterpass
masterpass
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cerixus

ASKER

Yeah, I thought about doing it with javascript again, but I was hoping there was a simpler, .net way.  I guess it's not a big deal either way...
Avatar of Cerixus

ASKER

And I get "Undetermined String Constant" on that function for some reason...
Try putting a space between = and 1 (it was together initially)

document.cookie=c_name+ "= 1" + ((expiredays==null) ? "" : ";
Avatar of Cerixus

ASKER

Changed to attached and it works great

(put on same line and added a semicolon after setting the value (1)
    function LoadInitialTab()
    {
        var expiredays =30;
        var c_name="ActiveTab";
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=1;" + ((expiredays==null) ? "" : "expires="+exdate.toGMTString());
    }

Open in new window

Good work Cerixus :)