[英]ASP.NET set default value for cookies
So i made 2 pages, one for settings and one for label and images. 所以我做了两页,一页用于设置,一页用于标签和图像。 the thing that i want is when there is no cookies the img_gm.BorderWidth and img_gm.Bordercolor will be 1 and Black by default, but it seems error always occur in libe "img_gm.BorderWidth = Convert.ToInt32(cookie["width"]);"
我想要的是默认情况下没有cookie时img_gm.BorderWidth和img_gm.Bordercolor分别为1和黑色,但是似乎总是在libe中出现错误“ img_gm.BorderWidth = Convert.ToInt32(cookie [” width“] );“ any help will be appreciated.
任何帮助将不胜感激。
*i put text box, dropdown list and radio button in settings page to get an input of cookies["width"], cookies["color"] and cookies["font"] *我在设置页面中放置了文本框,下拉列表和单选按钮,以获取cookie [“ width”],cookies [“ color”]和cookie [“ font”]的输入
protected void Page_Load(object sender, EventArgs e) { 受保护的无效Page_Load(对象发送者,EventArgs e){
HttpCookie cookie = Request.Cookies["settings"];
if (Request.Cookies.AllKeys.Contains("settings") == null)
{
cookie["width"] = "1";
cookie["color"] = "Black";
cookie.Expires = DateTime.Now.AddDays(14);
Response.Cookies.Add(cookie);
}
else
{
img_gm.BorderWidth = Convert.ToInt32(cookie["width"]);
img_gm.BorderColor = System.Drawing.Color.FromName(cookie["color"]);
switch (cookie["font"])
{
case "Bold":
lbl_desc.Font.Bold = true;
break;
case "Italic":
lbl_desc.Font.Italic = true;
break;
case "Overline":
lbl_desc.Font.Overline = true;
break;
case "Underline":
lbl_desc.Font.Underline = true;
break;
}
}
A neat trick is to change them to Properties in your class: 一个巧妙的技巧是在您的班级中将它们更改为Properties:
public int CookieWidth {
get {
int width;
var str = String.Format("{0}", cookie["width"]);
if (!Integer.TryParse(str, width)) {
width = 1;
}
return width;
}
set {
cookie["width"] = value;
}
}
You can do that with Color, also. 您也可以使用“颜色”来实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.