使用单选按钮未将对象引用设置为对象的实例

时间:2012-11-20 03:52:21

标签: c# asp.net radio-button

我收到此错误,我认为它来自一个单选按钮。关于如何解决这个问题的任何想法?

这是aspx:

  <asp:RadioButton ID="radL" runat="server" GroupName="Gender" Checked="false" Text="L" AutoPostBack="true" />
   <asp:RadioButton ID="radP" runat="server" GroupName="Gender" Checked="false" Text="P" AutoPostBack="true" />

背后的代码:

   RadioButton radL = (RadioButton)editedItem.FindControl("radL");
   RadioButton radP = (RadioButton)editedItem.FindControl("radP");
   RadioButton JAN = (RadioButton)editedItem.FindControl("JAN");

   if (radL.Checked == true)
   {
       JAN = radL;
   }
   else
   {
       JAN = radP;
   }
   SqlConnection conn1 = BusinessTier.getConnection();
   conn1.Open();
   int flg = BusinessTier.SavePersonalInfo(conn1, 1, txtEmpNo.Text.ToString().Trim(), txtJobTitle.Text.ToString().Trim(), Convert.ToInt32(txtKod.Text.ToString().Trim()), txtJenis.Text.ToString().Trim(),
   txtNama.Text.ToString().Trim(), txtNokp.Text.ToString().Trim(), TLAHIR.SelectedDate.Value, Convert.ToInt32(txtUmur.Text.ToString().Trim()), JAN.Text.ToString().Trim(), txtRas.Text.ToString().Trim(), txtBadge.Text.ToString().Trim(), txtKerja.Text.ToString().Trim(),
   txtKodclin.Text.ToString().Trim(), txtKc.Text.ToString().Trim(), txtAdd1.Text.ToString().Trim(), txtAdd2.Text.ToString().Trim(), txtAdd3.Text.ToString().Trim(), txtAlgr.Text.ToString().Trim(), Session["sesUserID"].ToString(), "N");
   BusinessTier.DisposeConnection(conn1);

1 个答案:

答案 0 :(得分:0)

你应该在转换FindControl()语句的结果

之前检查null

例如此行

RadioButton JAN = (RadioButton)editedItem.FindControl("JAN");

应改为

RadioButton JAN;
if(editedItem.FindControl("JAN") != null)
{
   RadioButton JAN = (RadioButton)editedItem.FindControl("JAN");
}