如何修复“对象引用未设置到对象实例”错误。它指的是哪个对象? 代码:
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
获取年份返回当前年份,它显示在详细信息 View 文本框“PlantYear”中。我尝试使用上面的代码插入值。
感谢您的帮助。
最佳答案
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
关于asp.net - 当我尝试添加插入值到 asp.net 详细信息 View 中时,我得到 "Object reference not set to an instance of an object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34228484/