有一个GridView有两个列分别绑定两个控件,Cells[0]:checkbox,  Cells[1]:TextBox,我想让某一行的checkbox选中之后,该行的TextBox就为可读,
我是用JavaScript写了ReaderOnly(vl)过程,
在.CS文件中
protected void gvw_DataBound(object sender, EventArgs e)
{
   for (int i = 0; i < gvw.Rows.Count; i++)
   {
     CheckBox chkSelect = gvw.Rows[i].Cells[0].FindControl("chkSelect") as CheckBox;
     chkSelect.Attributes.Add("OnClick", "ReaderOnly(this);");
   }
}
为什么在最后一行老是会出现"未将对象引用设置到对象的实例"的错误呢?

解决方案 »

  1.   

    make sure the control is thereprotected void gvw_DataBound(object sender, EventArgs e)
    {
       for (int i = 0; i < gvw.Rows.Count; i++)
       {Response.Write(gvw.Rows[i].Cells[0].Controls[0].GetType().Name);     CheckBox chkSelect = gvw.Rows[i].Cells[0].FindControl("chkSelect") as CheckBox;
         if (chkSelect != null)
          chkSelect.Attributes.Add("OnClick", "ReaderOnly(this);");   }
    }
      

  2.   

    我确定CheckBox控件是放在Cells[0]里的,可是为什么到 if (chkSelect != null)
    这句时chkSelect = null 呢?
      

  3.   

    CheckBox 是客户段的控件,所以把上面的CheckBox 改成HtmlInputCheckBox就好了。