CustomValidator in GridView getting "no message" on errors

  • Thread starter Thread starter doug
  • Start date Start date
D

doug

I have a gridview and have placed customvalidator controls in it.
If I put a break on the sub to do custom validation, run the page, select a
row and press the EDIT button, putting me into Update/Cancel mode, I can edit
the date textbox putting in garbage and press update. It takes the
breakpoint, I step thru, it sets value.isValid=False, and when I exit
routine, same sub fires again (hits same break point) and flow values same
path. Garbage date value still there. If I continue on now, it displays the
gridview, original value of date textbox is back, my error message is not
displayed, but I'm still in Update/Cancel mode,not Edit mode so the update
didn't take place.

<asp:TemplateField HeaderText="Date Stamp"
SortExpression="date_stamp">
<EditItemTemplate>
<asp:CustomValidator ID="Date_Stamp_Update_Validator"
runat="server"
ControlToValidate="Date_Stamp_Update_Txt"
OnServerValidate="Date_Stamp_Update_Txt_Validate"
ValidateEmptyText="true"
Display="Static"
SetFocusOnError="true"
ErrorMessage="Must be YYYY-MM-DD!" ></asp:CustomValidator>
<asp:TextBox ID="Date_Stamp_Update_Txt" runat="server"
Text='<%# Bind("date_stamp") %>'
BorderStyle="Ridge" BackColor="LightYellow"
MaxLength="10" Width="65px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Date_Stamp_Label" runat="server"
Text='<%# Bind("date_stamp") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" VerticalAlign="Bottom" Width="9%"
Wrap="True"/>
<HeaderStyle VerticalAlign="Bottom" />
</asp:TemplateField>


Protected Sub Date_Stamp_Update_Txt_Validate(ByVal sender As Object, ByVal
value As ServerValidateEventArgs)
' Must be YYYY-MM-DD Format:
If value.Value.ToString.Length = 10 Then
Dim dte As DateTime
If DateTime.TryParse(value.Value, dte) Then
value.IsValid = True
Else
value.IsValid = False
End If
Else
value.IsValid = False
End If
End Sub
 
Hi dmartin,

From your description, you found that the custom validator in GridView not
work correctly , correct?

I've made a simple test on my side and got a customValidator working (with
server-side validation) in GridView templateField. I've compared the code
and aspx template between my test page and yours. It seems there is no
definte difference. I suggest you try the following things:

** explicitly turn off client-side validation(disable client script)

** Try changing your validation handler in codebehind, use some very simple
code logic to see whether it work.

Here is my test page's aspx and codebehind function for your reference:


=======aspx ....==
..............
<asp:TemplateField HeaderText="name" SortExpression="name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("name") %>'></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1"
runat="server" ErrorMessage="CustomValidator"
ControlToValidate="TextBox1"
EnableClientScript="False"

onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
=============


===========
protected void CustomValidator1_ServerValidate(object source,
ServerValidateEventArgs args)
{
if (!args.Value.Contains("Name"))
args.IsValid = false;
else args.IsValid = true;

}
=============

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 
I added EnableClientScript="false", already had the "onservervalidate=xxx".
This is the routine that is being fired twice, and each time isPostBack is
true, and it fails edit setting isValid="false".

Why is this routine fired twice - seemingly one after the other?
 
I figured it out. I am trying to dynamically modify filtering SQL to add
additional criteria, and added a DataBing to Page_PreRenderComplete - which
isn't working yet, so commenting it out restored errors.
 
Back
Top