繁体   English   中英

ASP.NET为cookie设置默认值

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM