Confusing results with CustomValidator and Datagrid controls

2

2obvious

Below is a cut-and-paste code example that runs. It demonstrates some
results which confuse me.

It uses a DataGrid to make a table with 12 rows, each containing a
TextBox and a CustomValidator. When a textbox is populated, the
validator should write out the index of the row that triggered it. And
it does--twice.

e.g. If you type something into the first row, you see "0 0." If you
type something into the first and third rows, you see "0 2 0 2." I
would only expect to see one set of results. (I only /want/ one set of
results.) Can anyone explain why I'm seeing /two/?


<%@ page language="vb" runat="server" Debug="true"%>

<script runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
If ( IsPostBack ) Then
Validate()
Else
Dim numArray(12) As Integer

OurDataGrid.DataSource = numArray
OurDataGrid.DataBind()
End If
End Sub

Sub WriteIndex(source as Object, value as ServerValidateEventArgs)
Response.Write(source.Parent.Parent.ItemIndex & "<br>")
End Sub
</script>

<html><body>

<form runat="server">
<asp:Label id="OurLabel" runat="server" /><br />

<asp:DataGrid id="OurDataGrid"
AutoGenerateColumns="false"
runat="server">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<asp:TextBox id="firstName" runat="server" />
<asp:CustomValidator id="OurCustomValidator"
ControlToValidate="firstName"
OnServerValidate="WriteIndex"
runat="server"></asp:CustomValidator>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

<p>
<asp:Button text="Submit" runat="server" />
</p>
</form>

</body></html>
 
J

jasonkester

Try stepping through it in the debugger. I bet you'll see that a
breakpoint set in WriteIndex will fire before a breakpoint set at
Validate() in your PageLoad.

As in, the custom validator will fire all by itself. You've instructed
it to fire again in your pageLoad, so you're seeing its results twice.

Good luck!
Jason
Expat Software Consulting Services
http://www.expatsoftware.com/
 
P

Peter Blum

Just a clarification.

The Button, LinkButton and ImageButton controls all automatically call
Page.Validate() for you when their CausesValidation property is true (which
is the default). They call it after Page_Load and immediately before calling
your Click event handler.

As you know from the answer below, by adding another call to Validate in
Page_Load, it is called twice. As a general rule, try to avoid calling
Validate in Page_Load. If CausesValidation is false, you should call it in
your Click post back event handler before saving data.

ALWAYS remember to check Page.IsValid is true in Click before saving.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top