看name是不是空值,应该怎么写

解决方案 »

  1.   

    C#:if (Request.Cookies["name"] == null)
    {
      //does not exist
    }
      

  2.   

    谢谢楼上的,你的写法和我现在的写法一样。但是出现下面的错误提示:
    未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 32:  <TD vAlign="top" align="center">
    行 33:  <TABLE cellPadding="3" width="100%">
    行 34:  <%if (Request.Cookies["system"].Value==null ||
    行 35:  Request.Cookies["system"].Value=="")
    行 36:  {%>
     
      

  3.   

    if(Request.Cookies["system"]!=null)
    {
      String SystemStr = Request.Cookies["system"].Value.ToString();
    }
      

  4.   

    if(System.Web.HttpContext.Current.Request.Cookies["system"]!=null)
    {
    String SystemStr = System.Web.HttpContext.Current.Request.Cookies["system"].Value.ToString();
    }
      

  5.   

    非常感谢net_lover(孟子E章) ,解决了我的问题。
    您能讲一下为什么这么写吗?
    System.Web.HttpContext.Current.是什么含义,我给COOKIES赋值为什么不需要?
    .Value.ToString()是把值转化为字符串?直接可以转成int类型吗?
      

  6.   

    if(Request.Cookies["system"]!=null)
    {
    String SystemStr = Request.Cookies["system"].Value.ToString();
    }
    也可以.
      

  7.   

    System.Web.HttpContext.Current
    获取当前 HTTP 请求的所有 HTTP 特定的信息在自定义类中应当这样写,如果在Page_load等中,Request.Cookies["system"]应当是可以的