Вопрос

this code is returning a NullReferenceException since the SelectedDataKey is not selected yet. How can I check if the the DataKey is selected or not..

    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;
         }                   
        }
Это было полезно?

Решение

you will need to use a different event. the databound event is for wiring the data the view. you are looking for an event to use the data after it's bound. most likely the SelectedIndexChanging and SelectedIndexChanged events

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top