我用的vs2005,写了一个基类PageBae
想使用Cookie进行取用户信息
写了个方法如下public int CookiesUserID
{
get
{
if (HttpContext.Current.Request.Cookies["UserID"] != null)
{
return int.Parse(HttpContext.Current.Request.Cookies["UserID"].ToString().Trim());
}
else
{
return -1;
}
} set
{
HttpContext.Current.Request.Cookies["UserID"] = value;
}
}在页面中继承了这个类,使用就是出错,如果换用Session就没有任何问题。
如果将set里去掉可运行但Cookie取不到数据
想使用Cookie进行取用户信息
写了个方法如下public int CookiesUserID
{
get
{
if (HttpContext.Current.Request.Cookies["UserID"] != null)
{
return int.Parse(HttpContext.Current.Request.Cookies["UserID"].ToString().Trim());
}
else
{
return -1;
}
} set
{
HttpContext.Current.Request.Cookies["UserID"] = value;
}
}在页面中继承了这个类,使用就是出错,如果换用Session就没有任何问题。
如果将set里去掉可运行但Cookie取不到数据
解决方案 »
- 页面弹出div层传递参数播放视频
- 求救!!asp.net 相对路径问题
- 请问如何实现异步发送邮件?
- 求问用什么控件好
- Page_Load事件为什么没有响应?
- PubConstant.cs不能获取到WEB.CONFIG 文件中的数据库连接字符串
- 求救 ,, VS调试 localhost 的端口不统一,,,
- 精通网页设计和ASP程序设计者请进来,特产旅游网站求合作
- 用datagrid的DataGrid1_DeleteCommand事件删除记录时如何加入提示?
- 问一下dataGrid的问题
- "必须声明变量 '@jobid' 求救?
- asp.net中innerhtml,<%=***%>,response.write的问题
:public int CookiesUserID
{
get
{
if (HttpContext.Current.Request.Cookies["myCook"] != null)
{
return int.Parse(HttpContext.Current.Request.Cookies["myCook"]["UserID"].ToString().Trim());
}
else
{
return -1;
}
} set
{
if (HttpContext.Current.Request.Cookies["myCook"] != null)
{
HttpContext.Current.Request.Cookies["myCook"]["UserID"] = value;
}else
{
HttpContext.CuRENT.Response.Cookies.Add(new HttpCookie["myCook"]);
HttpContext.CuRENT.Response.Cookies["myCook"]["UserID"] = value;}
}
}-----难免有错,错了莫怪.
Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string'Source Error: Line 43: if (HttpContext.Current.Request.Cookies["myCook"] != null)
Line 44: {
Line 45: HttpContext.Current.Request.Cookies["myCook"]["UserID"] = value;
Line 46: }45行出错
出错提示如下 CS0131: The left-hand side of an assignment must be a variable, property or indexerSource Error: Line 43: if (HttpContext.Current.Request.Cookies["myCook"] != null)
Line 44: {
Line 45: int.Parse(HttpContext.Current.Request.Cookies["myCook"]["UserID"].ToString().Trim()) = value;
Line 46: }
Line 47: else
{
get
{
if (HttpContext.Current.Request.Cookies["myCook"] != null)
{
return int.Parse(HttpContext.Current.Request.Cookies["myCook"]["UserID"].ToString().Trim());
}
else
{
return -1;
}
} set
{
if (HttpContext.Current.Request.Cookies["myCook"] != null)
{
HttpContext.Current.Request.Cookies["myCook"]["UserID"] = value.ToString();
}
else
{
HttpContext.Current.Response.Cookies.Add(new HttpCookie("myCook"));
HttpContext.Current.Response.Cookies["myCook"]["UserID"] = value.ToString(); }
}
}-----难免有错,错了莫怪.
再请问一下如何设置时间呢?比如说保存一个星期一个月
{
HttpContext.Current.Response.Cookies.Add(new HttpCookie("myCook"));
HttpContext.Current.Response.Cookies["myCook"]["UserID"] = value.ToString(); }将HttpContext.Current.Response.Cookies.Add(new HttpCookie("myCook"));去掉就可以了。
HttpCookie cook = new HttpCookie("myCook");
cook.Expires = DateTime.Now.AddMonths(1); HttpContext.Current.Response.Cookies.Add(cook);
HttpContext.Current.Response.Cookies["myCook"]["UserID"] = value.ToString();-----难免有错,错了莫怪.