0
 protected void EditButton_Click(object sender, EventArgs e)
    {

        TextBox tdcd = (TextBox)FormViewDiagnostic.FindControl("DIAG_COMPL_DATETextBox");
        TextBox tdrcd = (TextBox)FormViewDiagnostic.FindControl("DIAG_REVIEW_COMPL_DATETextBox");
        RadioButtonList rbl =(RadioButtonList)FormViewDiagnostic.FindControl("DIAG_LL_APPROVALRadioButtonList");
        TextBox tll = (TextBox)FormViewDiagnostic.FindControl("DIAG_LL_COMMENTSTextBox");

        //if (!"".Equals(tdcd) && !"".Equals(tdrcd))

        if (!string.IsNullOrEmpty(tdcd.Text) && !string.IsNullOrEmpty(tdrcd.Text))
        {

            //tdcd.Visible = true;
            //tdrcd.Visible = true;

            FormViewDiagnostic.FindControl("DIAG_LL_APPROVALRadioButtonList").Visible = true;
            FormViewDiagnostic.FindControl("DIAG_LL_COMMENTSTextBox").Visible = true;
        }
        else
        {
            //tdcd.Visible = false;
            //tdrcd.Visible = false;
            FormViewDiagnostic.FindControl("DIAG_LL_APPROVALRadioButtonList").Visible = false;
            FormViewDiagnostic.FindControl("DIAG_LL_COMMENTSTextBox").Visible = false;
        }
    }

对象引用未设置为行中对象的实例

if (!string.IsNullOrEmpty(tdcd.Text) && !string.IsNullOrEmpty(tdrcd.Text))
4

5 回答 5

1

您的tdcdortdrcd为空。这就是为什么你会得到这个例外。您可以先检查它们是否为空。

if ((tdcd != null && tdrcd!=null) && (!string.IsNullOrEmpty(tdcd.Text) && !string.IsNullOrEmpty(tdrcd.Text)))
于 2012-07-06T10:55:08.283 回答
1

tdcd或者tdrcdnull您搜索它,并且不能保证您总是会得到它。

TextBox tdcd = (TextBox)FormViewDiagnostic.FindControl("DIAG_COMPL_DATETextBox");
TextBox tdrcd = (TextBox)FormViewDiagnostic.FindControl("DIAG_REVIEW_COMPL_DATETextBox");
于 2012-07-06T10:56:56.223 回答
1

tdcdtdrcd为空。这意味着FormViewDiagnostic.FindControl()为其中一个返回 null 。

这可能意味着或者"DIAG_COMPL_DATETextBox"不是"DIAG_REVIEW_COMPL_DATETextBox"控件的正确 ID。

检查这些 ID 是否与表单上实际声明的内容相匹配。

于 2012-07-06T11:01:03.863 回答
0

TextBox引用(和tdcd/或tdrcd)为空。

您应该使用FormView.ItemDataBound适当的事件FormViewMode(检查 的CurrentMode属性FormView)来获取对控件的引用。

您还需要DataBind以前的 FormView 来创建控件。

例如(假设TextBoxes在 The 中EditItemTenmplate):

protected void EditButton_Click(object sender, EventArgs e)
{
    Object someSource=null;
    FormViewDiagnostic.ChangeMode(FormViewMode.Edit);
    FormViewDiagnostic.DataSource = someSource;
    FormViewDiagnostic.DataBind();
}


protected void FormViewDiagnostic_DataBound(Object sender, EventArgs e)
{
    var view = (FormView)sender;
    if (view.CurrentMode == FormViewMode.Edit)
    {
        var txt1 = (TextBox)view.FindControl("DIAG_COMPL_DATETextBox");
        var txt2 = (TextBox)view.FindControl("DIAG_REVIEW_COMPL_DATETextBox");
        var txt3 = (TextBox)view.FindControl("DIAG_LL_COMMENTSTextBox");
        var rbl = (RadioButtonList)view.FindControl("DIAG_LL_APPROVALRadioButtonList");
        txt3.Enabled = rbl.Enabled = txt1.Text.Length != 0 && txt2.Text.Length != 0;
    }
}
于 2012-07-06T11:08:13.847 回答
0

做这样的检查

if(tdcd != null && tdrcd != null)
{
// do stuff
}
于 2012-07-06T10:56:32.763 回答