0

此代码返回 NullReferenceException,因为尚未选择 SelectedDataKey。如何检查是否选择了 DataKey ..

    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;
         }                   
        }
4

1 回答 1

0

您将需要使用不同的事件。数据绑定事件用于将数据连接到视图。您正在寻找一个事件以在绑定后使用数据。很可能是SelectedIndexChangingSelectedIndexChanged事件

于 2012-01-03T19:03:24.783 回答