473,433 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,433 software developers and data experts.

Confusing results with CustomValidator and Datagrid controls

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>

Nov 19 '05 #1
2 1406
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/

Nov 19 '05 #2
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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"jasonkester" <ja*********@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
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/

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: jcpmeticulus | last post by:
Hi I've spent the last day or so debugging a problem with a CustomValidator and am now totally stumped! Basically I use a number of CustomValidator's on my page, but have cut this down now to...
1
by: Jose Fernandez | last post by:
Hola I have this customvalidator <asp:CustomValidator id="ComboValidator" runat="server" ErrorMessage="CustomValidator" /> (The OnServerValidate method is set in Events windows of the Control...
1
by: Dave | last post by:
I have added a custom validator to the EditItemTemplate of a DataGrid as follows: <EditItemTemplate> <asp:TextBox id=txtWorkDate CssClass="ResultsDim" runat="server" Width="100%" Text='<%#...
5
by: Gene McCrory | last post by:
Is it possible to have a CustomValidator against two controls in an EditItemTemplate on a DataGrid? Case: Have a business rule that states if ATextBox starts with "ACertainValue" then BTextBox...
3
by: philipl | last post by:
hi, i have a textbox in my edititemtemplate in my datagrid. I have created a customvalidator for it, when i update the row the appropiate function gets called to customvalidate but the problem...
1
by: 2obvious | last post by:
I'm using a CustomValidator within a datagrid control. I'd like to get the index of the item (i.e. row of the datagrid) when the validator is fired and use this number in my custom function. Can...
1
by: Vi | last post by:
Hi, I have two pairs of a DropDownList and TextBox Controls and for each pair I have a CustomValidator that makes sure that at least one of the two (DropDownList or TextList) has a value. When I...
0
by: ghafranabbas | last post by:
This is how you use the customvalidator control in any INamingContainer interface control (Datagrid, DataList, DataRepeater, etc). 1. In the ItemTemplate, place your customvalidator 2. Set the...
6
by: Dabbler | last post by:
I'm trying to use a custom validator but it seems to trigger no matter what I do. I'm not sure i'm referencing the controls involved correctly from the js. My FormView is named fvRegDetail. I've...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.