[英]ASP.NET set default value for cookies
所以我做了兩頁,一頁用於設置,一頁用於標簽和圖像。 我想要的是默認情況下沒有cookie時img_gm.BorderWidth和img_gm.Bordercolor分別為1和黑色,但是似乎總是在libe中出現錯誤“ img_gm.BorderWidth = Convert.ToInt32(cookie [” width“] );“ 任何幫助將不勝感激。
*我在設置頁面中放置了文本框,下拉列表和單選按鈕,以獲取cookie [“ width”],cookies [“ color”]和cookie [“ font”]的輸入
受保護的無效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;
}
}
一個巧妙的技巧是在您的班級中將它們更改為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;
}
}
您也可以使用“顏色”來實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.