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"]);" 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"]
protected void Page_Load(object sender, 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:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.