当我尝试在 asp.net Detailsview 中添加插入值时,我得到了 "Object reference not set to an instance of an object"
When I try add insert a value into a asp.net Detailsview I get a "Object reference not set to an instance of an object"
我该怎么做才能修复 "Object reference not set to an instance of an object" 错误。它指的是哪个对象?
代码:
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
获取年份returns为当前年份,它出现在detailsview文本框中"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
我该怎么做才能修复 "Object reference not set to an instance of an object" 错误。它指的是哪个对象? 代码:
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
获取年份returns为当前年份,它出现在detailsview文本框中"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