如何修复未设置为对象实例的"对象引用"错误。它指的是哪个对象? 代码:
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"。我尝试使用上面的代码插入值。
感谢您的帮助。
答案 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