Cerixus
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(aCook ie)
Method 2:
Response.Cookies.Add(New HttpCookie("ActiveTab", "1"))
Method 3:
Response.Cookies("ActiveTa b").Value = "1"
Response.Cookies("ActiveTa b").Expire s = 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(myCoo kie)
End If
Method 1:
Dim aCookie As New HttpCookie("ActiveTab")
aCookie.Value = "1"
aCookie.Expires = DateTime.Now.AddDays(30)
Response.Cookies.Add(aCook
Method 2:
Response.Cookies.Add(New HttpCookie("ActiveTab", "1"))
Method 3:
Response.Cookies("ActiveTa
Response.Cookies("ActiveTa
Method 4: (Just expire the cookie)
If (Not Request.Cookies("ActiveTab
Dim myCookie As HttpCookie
myCookie = New HttpCookie("ActiveTab")
myCookie.Expires = DateTime.Now.AddDays(-1D)
Response.Cookies.Add(myCoo
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());
}
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
If(!IsPostBack)
{
TabPanel1.ActiveTabIndex = 1;
}
just see if it works .. other wise we ll go for cookie overwrite
ASKER
No, that won't work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...
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) ? "" : ";
document.cookie=c_name+ "= 1" + ((expiredays==null) ? "" : ";
ASKER
Changed to attached and it works great
(put on same line and added a semicolon after setting the value (1)
(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());
}
Good work Cerixus :)
ASKER