当我尝试在asp.net Detailsview中添加一个值时,我得到一个" Object引用未设置为对象的实例"

时间:2015-12-11 16:46:01

标签: asp.net .net vb.net detailsview

如何修复未设置为对象实例的"对象引用"错误。它指的是哪个对象? 代码:

Private Sub dvSMasterCurrentYear_DataBound(sender As Object, e As EventArgs) Handles dvSMasterCurrentYear.DataBound
    Dim dv As DetailsView = New DetailsView
    If DetailsViewMode.Insert Then
        DirectCast(dv.FindControl("PlantYear"), TextBox).Text = GetYear()
    End If
End Sub

将年份返回到当前年份,它将显示在详细信息视图文本框中" PlantYear"。我尝试使用上面的代码插入值。

感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

最有可能的FindControl实际上并没有找到控件。检查是否明智,以确保它实际上找到了您想要找到的内容:

Private Sub dvSMasterCurrentYear_DataBound(sender As Object, e As EventArgs) Handles dvSMasterCurrentYear.DataBound
    Dim dv As DetailsView = New DetailsView
    If DetailsViewMode.Insert Then
        Dim ctl = dv.FindControl("PlantYear")
        If ctl IsNot Nothing Then
            DirectCast(dv.FindControl("PlantYear"), TextBox).Text = GetYear()
        Else
            Throw New Exception("Control was not found")
        End If        
    End If
End Sub