Discussion:
GridView and Page.IsValid
Adam Sills
2007-09-10 15:29:54 UTC
Permalink
I think most people (myself included) would like to skip validation because
typically the Delete button should avoid all input validation that is
associated with the current editing row (since you're deleting the row
anyway).

Adam..

-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
[mailto:DOTNET-***@DISCUSS.DEVELOP.COM] On Behalf Of Mark Brackett
Sent: Monday, September 10, 2007 9:38 AM
To: DOTNET-***@DISCUSS.DEVELOP.COM
Subject: [DOTNET-WEB] GridView and Page.IsValid

The GridView seems to check Page.IsValid before handling an Update
(reflected C#):

private void HandleUpdate(GridViewRow row, int rowIndex, bool
causesValidation) {
if ((!causesValidation || (this.Page == null)) ||
this.Page.IsValid) {
// ... do update to datasource ... //
}
}

But not on Delete. It calls Page.Validate() in bool
HandleEvent(EventArgs e, bool causesValidation, string validationGroup),
but only actually checks Page.IsValid in HandleUpdate(). Personally, I
think it should be checking IsValid on both Delete and Edit commands at
a minimum....

This seems like a huge oversight to me - is everyone hooking a handler
to GridView.RowDeleting and setting e.Cancel = !IsValid?

===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Mark Brackett
2007-09-10 14:37:30 UTC
Permalink
The GridView seems to check Page.IsValid before handling an Update
(reflected C#):

private void HandleUpdate(GridViewRow row, int rowIndex, bool
causesValidation) {
if ((!causesValidation || (this.Page == null)) ||
this.Page.IsValid) {
// ... do update to datasource ... //
}
}

But not on Delete. It calls Page.Validate() in bool
HandleEvent(EventArgs e, bool causesValidation, string validationGroup),
but only actually checks Page.IsValid in HandleUpdate(). Personally, I
think it should be checking IsValid on both Delete and Edit commands at
a minimum....

This seems like a huge oversight to me - is everyone hooking a handler
to GridView.RowDeleting and setting e.Cancel = !IsValid?

--Mark Brackett

===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Mark Brackett
2007-09-12 18:35:16 UTC
Permalink
I think the better way to handle that is to just set
CausesValidation="false" on the delete button. In that spirit, here's my
subclassed GridView:

Public Class ValidatingGridView
Inherits GridView

Protected Overrides Sub OnRowEditing(ByVal e As
System.Web.UI.WebControls.GridViewEditEventArgs)
If IsValid() Then
MyBase.OnRowEditing(e)
Else
e.Cancel = True
End If
End Sub

Protected Overrides Sub OnRowDeleting(ByVal e As
System.Web.UI.WebControls.GridViewDeleteEventArgs)
If IsValid() Then
MyBase.OnRowDeleting(e)
Else
e.Cancel = True
End If
End Sub

Protected Function IsValid() As Boolean
If Not _causesValidation OrElse Page Is Nothing OrElse
Page.IsValid Then
Return True
Else
Return False
End If
End Function

Protected Overrides Function OnBubbleEvent(ByVal source As
Object, ByVal e As System.EventArgs) As Boolean
' Capture validation group and causes validation from the
event source
Dim cmdEventArgs As GridViewCommandEventArgs = TryCast(e,
GridViewCommandEventArgs)
If cmdEventArgs IsNot Nothing Then
Dim cmdSource As IButtonControl =
TryCast(cmdEventArgs.CommandSource, IButtonControl)
If cmdSource IsNot Nothing Then
_causesValidation = cmdSource.CausesValidation
_validationGroup = cmdSource.ValidationGroup
End If
End If

Return MyBase.OnBubbleEvent(source, e)
End Function

Private _causesValidation As Boolean
Private _validationGroup As String
End Class

--MB
Post by Adam Sills
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Monday, September 10, 2007 11:30 AM
Subject: Re: [DOTNET-WEB] GridView and Page.IsValid
I think most people (myself included) would like to skip validation
because
typically the Delete button should avoid all input validation that is
associated with the current editing row (since you're deleting the row
anyway).
Adam..
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: Monday, September 10, 2007 9:38 AM
Subject: [DOTNET-WEB] GridView and Page.IsValid
The GridView seems to check Page.IsValid before handling an Update
private void HandleUpdate(GridViewRow row, int rowIndex, bool
causesValidation) {
if ((!causesValidation || (this.Page == null)) ||
this.Page.IsValid) {
// ... do update to datasource ... //
}
}
But not on Delete. It calls Page.Validate() in bool
HandleEvent(EventArgs e, bool causesValidation, string
validationGroup),
but only actually checks Page.IsValid in HandleUpdate(). Personally, I
think it should be checking IsValid on both Delete and Edit commands
at
Post by Adam Sills
a minimum....
This seems like a huge oversight to me - is everyone hooking a handler
to GridView.RowDeleting and setting e.Cancel = !IsValid?
===================================
This list is hosted by DevelopMentor(r) http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Loading...