PC Review


Reply
Thread Tools Rate Thread

Confusing results with CustomValidator and Datagrid controls

 
 
2obvious
Guest
Posts: n/a
 
      2nd Jan 2005
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 />

<aspataGrid 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>
</aspataGrid>

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

</body></html>

 
Reply With Quote
 
 
 
 
jasonkester
Guest
Posts: n/a
 
      2nd Jan 2005
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/

 
Reply With Quote
 
Peter Blum
Guest
Posts: n/a
 
      3rd Jan 2005
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 Removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"jasonkester" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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/
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CustomValidator Multiple Controls? Hillbilly Microsoft ASP .NET 0 3rd Oct 2008 04:18 PM
Reuse CustomValidator controls Joo Park Microsoft ASP .NET 0 16th Oct 2004 01:27 AM
CustomValidator against two controls in EditItemTemplate on a DataGrid? Gene McCrory Microsoft ASP .NET 5 12th Dec 2003 06:58 AM
customValidator in DATAGRID Christophe Microsoft ASP .NET 0 11th Aug 2003 09:28 AM
CustomValidator controls George Ter-Saakov Microsoft ASP .NET 0 21st Jul 2003 10:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:14 AM.