DataGrid中的CheckBox相關代碼

private void Button1_Click(object sender, System.EventArgs e)
  {
   CheckBox c1;// = new CheckBox();
   for(int i =0;i<dg1.Items.Count;i++)
   {
    c1 =(CheckBox)dg1.Items[i].FindControl("CheckBox1");
    if(c1 !=null && c1.Checked )
    {
     Label1.Text = Label1.Text +dg1.Items[i].Cells[1].Text;
     Label2.Text = Label2.Text +dg1.Items[i].Cells[2].Text;
    }
   }

  注意 c1 != null 這條判斷,如果沒有加這條判斷,則會發生 Null 的異常錯誤

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章