在讨论中关注我在stackoverflow中的上一个帮助: Show a div in asp.net on condition
我有一个ASP.NET页面,其中包含一个HTML格式的webform,其中包含javascript代码集cookie。 (我知道javascript正在设置cookie,因为我可以通过ctrl + shift + I的组合在谷歌浏览器中看到它们。)
我在这个解决方案背后的代码中写道,为了显示/不显示基于值cooke set的表单:
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie myCookie = new HttpCookie("tempcookieforclose");
myCookie = Request.Cookies["tempcookieforclose"];
// Read the cookie information and display it.
if (myCookie != null)
webform.Visible = false;
else
webform.Visible = true;
这不起作用。如果在输出中设置了cookie名称,那么这个例子也不起作用:
HttpCookie myCookie = new HttpCookie("MyTestCookie");
myCookie = Request.Cookies["MyTestCookie"];
// Read the cookie information and display it.
if (myCookie != null)
Response.Write("<p>"+ myCookie.Name + "<p>"+ myCookie.Value);
else
Response.Write("not found");
(我改名为MyTestCookie,名字为&#34; mycookie&#34;)
我也发布了为我的webform设置cookie的函数:
function tempcookie() {
days = 1; // number of days to keep the cookie
myDate = new Date();
myDate.setTime(myDate.getTime() + (days * 24 * 60 * 60 * 1000));
document.cookie = 'cookieName=cookieclose; expires=' + myDate.toGMTString();
function permacookie() {
days = 30; // number of days to keep the cookie
myDate = new Date();
myDate.setTime(myDate.getTime() + (days * 24 * 60 * 60 * 1000));
document.cookie = 'cookieName=cookiesignup; expires=' + myDate.toGMTString();
}
答案 0 :(得分:1)
我在你的代码中做了一些修改。我希望它会对你有所帮助。
HttpCookie myCookie = new HttpCookie("tempcookieforclose");
myCookie.Values.Add("date", "1");
Response.Cookies.Add(myCookie);
// Read the cookie information and display it.
myCookie = Request.Cookies["tempcookieforclose"];
if (myCookie != null)
Response.Write("In If");
else
Response.Write("In Else");