2012-01-03 17 views
0

由於未選擇SelectedDataKey,因此此代碼返回NullReferenceException。如何檢查DataKey是否被選中。在RowDataBound中獲取SelectedDataKey

protected void grdRoles_RowDataBound1(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      CheckBox chk = (CheckBox)e.Row.FindControl("chkRole"); 
      int rolecode = Convert.ToInt32(this.grdRoles.DataKeys[e.Row.RowIndex].Value); 
      BusinessLayer.Customers checkRole = new BusinessLayer.Customers();     

     bool check = checkRole.CheckRoles(this.grdCustomers.SelectedDataKey.Value.ToString(), rolecode); //this is triggering the nullReferenceException 
     if (check) 
     { 
      chk.Checked = true; 
     }     
     } 

回答

0

您將需要使用不同的事件。數據綁定事件用於將數據連接到視圖。您正在查找一個事件以在綁定後使用這些數據。最有可能的是SelectedIndexChangingSelectedIndexChanged事件

相關問題