Tips and tricks for .NET using ASP and VB code.

Check for read-only or hidden fields in DetailsView that are not in the DataKey collection.

    Protected Sub DetailsView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.PreRender
Try
'Catch error when an old .NET Framework doesn't have a DetailsView.DataSourceObject available.
Dim FrameworkErrorCatcher As Object = sender.DataSourceObject
Catch
Exit Sub
End Try
If TypeOf sender.DataSourceObject Is ObjectDataSource Then
Dim DetailsViewDataSource As ObjectDataSource = sender.DataSourceObject
Dim SourceUpdateMethodName As String = DetailsViewDataSource.UpdateMethod
If String.IsNullOrEmpty(SourceUpdateMethodName) = False Then
For Each DetailsViewDataControlField As DataControlField In sender.Fields
If TypeOf DetailsViewDataControlField Is BoundField Then
Dim DetailsViewBoundField As BoundField = DetailsViewDataControlField
Dim FieldName As String = DetailsViewBoundField.DataField
If DetailsViewBoundField.ReadOnly = True Or DetailsViewBoundField.Visible = False Then
Dim DataKeyIndex As Integer = Array.IndexOf(sender.DataKeyNames, FieldName)
If DataKeyIndex = -1 Then
Throw New ApplicationException(String.Format("Read-only or hidden field {0} is not in the {1} DateKey collection. This field will not be returned during updates.", FieldName, sender.ID))
End If
End If
End If
Next DetailsViewDataControlField
End If
End If
End Sub

No comments:

Post a Comment

Search This Blog