-2

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

这是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);
4

1 回答 1

0

您应该在转换 FindControl() 语句的结果之前检查 null

例如这条线

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

应该改为

RadioButton JAN;
if(editedItem.FindControl("JAN") != null)
{
   RadioButton JAN = (RadioButton)editedItem.FindControl("JAN");
}
于 2012-11-20T07:07:04.193 回答