在test.aspx中有这样一个checkbox
<form name="form1" method="post" action="Default.aspx"> 
<input type="checkbox" name="check_id" size="6" value="1" > 
<input type="submit" name="ok" value="确定">
</form>然后在Default.aspx.cs中写了这么一个函数
public string test()
{
   string i = Request.Form["check_id"].ToString();
   if (i==""||i==null)
   {
       return "aaa";
   }
       return i;
}
我的意思是如果这个checkbox选中了,显示value的值,未选中就显示空。可是当我不选中的话,按下按钮,错误信息如下:
System.NullReferenceException: Object reference not set to an instance of an object.
为什么不行呢??有办法解决吗,大家帮忙啊!!!!!!!!!

解决方案 »

  1.   

    应该没问题啊,你在Default.aspx.cs是怎么调用test()的?
      

  2.   

    <%# test() %>
    就这样啊,如果我选中的话是可以显示的,但是不选就报错,为什么啊?
      

  3.   

    哦,知道了,没选择check_id的话这个Request.Form["check_id"]就返回null,就不能调用ToString()方法了,直接去掉.ToString(),这样写:
    public string test()
    {
       string i = Request.Form["check_id"];
       if (i==""||i==null)
       {
           return "aaa";
       }
           return i;
    }