Get SelectedDataKey in RowDataBound

偶尔善良 提交于 2019-12-13 07:00:18

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/8717175/get-selecteddatakey-in-rowdatabound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!