用户代码asp.net未处理nullreferenceexception [英] nullreferenceexception was unhandled by user code asp.net

查看:121
本文介绍了用户代码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;
              }

          //}
      }
  }

推荐答案

此异常显示是因为控件无法检测到。

表示:无法呈现控件。



检查名称rbYes是否正确。



休息看起来没问题。
This Exception is showing because the control is unable to detect.
Means:unable to render the control.

Check the name "rbYes" is correct or not.

rest looks ok.


FindControl 可能找不到您在该行指定的名称:

The FindControl is probably not finding the name you specified at the line :
RadioButton rbYes = editItem.FindControl("rbYes");



检查名称rbYes是否正确。


Check the name "rbYes" is correct.


基本上,您的代码无法找到名为rbYes的控件。

Basically, your code failed to find a control called "rbYes".
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆