2011-08-19 83 views
2

我想实现的GridView中的复选框,GridView的复选框问题asp.net

此复选框的任务是验证记录,

当此验证按钮被按下,用检查所有项目复选框被输入到数据库中

这是我的代码:

protected void Button1_Click(object sender, EventArgs e) 
    { 

     foreach (GridViewRow row in GridView1.Rows) 
     { 

      CheckBox cbox = ((CheckBox)row.FindControl("Verify")); 

      if (cbox.Equals(true)) 
      { 
       String DraftsText = ((TextBox)row.FindControl("numDrafts")).Text; 
       String TCtext = ((TextBox)row.FindControl("numTC")).Text; 

       if (row.RowType == DataControlRowType.DataRow) 
       { 
        //Header trs = new Header(); 
        // GridView1.Rows[0].FindControl("numTC"); 
        if (TCtext != "" && DraftsText != "") 
        { 

         // try 
         // { 
         string date = row.Cells[4].Text; 

         DateTime dateTime = Convert.ToDateTime(date); 
         string dateFormatted = dateTime.ToString("d-MMM-yy"); 

         string unit = row.Cells[5].Text; 
         string currency = row.Cells[6].Text; 
         string totalFC = row.Cells[7].Text; 
         string totalDC = row.Cells[8].Text; 
         int d = Convert.ToInt32(DraftsText); 
         int tc = Convert.ToInt32(TCtext); 


         hdr = new Header(d, tc, dateFormatted, unit, currency, totalFC, totalDC); 
         hdr.InsertFCTC(hdr); 
        } 

       } 

      } 
     } 
    } 

我可能会在这个错误的方式,但在如果(cbox.Equals(真))可去它给了我一个例外:对象引用未设置为对象的实例。

任何想法我能做些什么来解决这个问题?

非常感谢

回答

1

if (cbox.Equals(true))应该if (cbox.Checked)

由于您cbox is a checkbox object它不能被用来比较,所以你必须使用cboxChecked属性,它会返回true/false

+0

仍然给我对象引用不设置到对象的实例。 – Karl

+0

当您尝试查找使用row.FindControl时,未找到该复选框...您必须发布aspx标记以查找问题。 –

+0

好的问题解决了,我通过了错误的文本框ID&如果应该已经如你所说感谢 – Karl

1

你收到一个NullPointerException,因为没有找到建议的复选框!或者直接强制转换为CheckBox类型的实例不能按预期工作。

+0

好的问题解决了,我传递错误的文本框ID&如果应该已经如果(cbox.Checked) – Karl

1

更改您的代码是这样的,然后重试:

CheckBox cbox = ((CheckBox)row.FindControl("Verify")); 

      if (cbox != null && cbox.Checked) 
      { 
.... 
}