net未处理nullreferenceexception

net未处理nullreferenceexception

本文介绍了用户代码asp.net未处理nullreferenceexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在下面编写代码时,我在rbYes.Checked == true时得到错误,因为nullreferenceexception未被用户代码asp.net处理

whenever i am excuting the below code am getting the error at the rbYes.Checked==true as nullreferenceexception was unhandled by user code asp.net

protected void rgDesignation_ItemCommand(object source, GridCommandEventArgs e)
  {
      if (e.CommandName == "InitInsert")
      {
          for (int k = 0; k < rgDesignation.Items.Count; k++)
          {
              if (rgDesignation.Items[k].Edit)
              {
                  e.Canceled = true;
                  return;
              }
          }
      }
      if (e.CommandName == "Edit")
      {
          if (rgDesignation.MasterTableView.IsItemInserted)
          {
              e.Canceled = true;
              return;
          }
          GridEditableItem editItem = (GridEditableItem)e.Item;
          RadTextBox txDesignation = (RadTextBox)editItem.FindControl("txDesignation");

          RadioButton rbYes = (RadioButton)editItem.FindControl("rbYes");
          RadioButton rbNo = (RadioButton)editItem.FindControl("rbNo");
          RadioButton rbUser = (RadioButton)editItem.FindControl("rbUser");
          int DesgID = (int)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["DesgID"];
          DataView dv;
          DataSet ds = new DataSet();
          ds = DataAccessLayer.BindDataQuery("select Estatus1 from tblDesignations where DesgID='" + DesgID + "'");
          dv = ds.Tables[0].DefaultView;
          int Name;
          // args.IsValid = true;

          //foreach (DataRowView datarow in dv)
          //{
              int a = 1, b = 2, c = 3;
              Name = Convert.ToInt32(ds.Tables[0].Rows[0]["Estatus1"]);
              if (Name == a)
              {
                  rbYes.Checked = true;
                  rbNo.Checked = false;
                  rbUser.Checked = false;
              }
              else if (Name==b)
              {
                  rbNo.Checked = true;
                  rbYes.Checked = false;
                  rbUser.Checked = false;
              }
              else
              {
                  rbUser.Checked = true;
                  rbNo.Checked = false;
                  rbYes.Checked = false;
              }

          //}
      }
  }

推荐答案


RadioButton rbYes = editItem.FindControl("rbYes");



检查名称rbYes是否正确。


Check the name "rbYes" is correct.


RadioButton rbYes = (RadioButton)editItem.FindControl("rbYes");

检查一下r editItem 实际上包含了这个,或者你拼写正确。

Check that your editItem actually contains this, or that you have spelled it correctly.


这篇关于用户代码asp.net未处理nullreferenceexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:18