473,804 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(sende r as Object, e as EventArgs)
If ( IsPostBack ) Then
Validate()
Else
Dim numArray(12) As Integer

OurDataGrid.Dat aSource = numArray
OurDataGrid.Dat aBind()
End If
End Sub

Sub WriteIndex(sour ce as Object, value as ServerValidateE ventArgs)
Response.Write( source.Parent.P arent.ItemIndex & "<br>")
End Sub
</script>

<html><body>

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

<asp:DataGrid id="OurDataGrid "
AutoGenerateCol umns="false"
runat="server">
<Columns>
<asp:TemplateCo lumn>
<HeaderTemplate ></HeaderTemplate>
<ItemTemplate >
<asp:TextBox id="firstName" runat="server" />
<asp:CustomVali dator id="OurCustomVa lidator"
ControlToValida te="firstName"
OnServerValidat e="WriteIndex "
runat="server"> </asp:CustomValid ator>
</ItemTemplate>
<FooterTemplate ></FooterTemplate>
</asp:TemplateCol umn>
</Columns>
</asp:DataGrid>

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

</body></html>

Nov 19 '05 #1
2 1423
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 CausesValidatio n 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 CausesValidatio n 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****@PeterBlu m.com
Creator of "Profession al Validation And More" at
http://www.peterblum.com/vam/home.aspx

"jasonkeste r" <ja*********@gm ail.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.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
2179
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 only 2. When the page is first loaded the CustomValidator are made and the generated HTML contains both of them. When the "Apply" button is clicked the page is posted back and my database is updated. The page is then rebuilt and sent back to the...
1
1376
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 Property) THIS IS THE METHOD
1
1836
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='<%# DataBinder.Eval(Container, "DataItem.WorkDate","{0:g}") %>' > </asp:TextBox> <asp:requiredfieldvalidator id="RfvWorkDate" runat="server" ErrorMessage="Enter the Work Date" ControlToValidate="txtWorkDate"
5
3167
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 value is required. I added a CustomValidator to the EditItemTemplate for the column containing BTextBox. I did not set the ControlToValidate, but added a Method for the OnServerValidate. The OnServerValidate is called correctly, but I don't...
3
1859
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 is that the error message in my customvalidator does not get printed in the datagrid even though I have set the isvalid property in the server function to false. Does anyone know why this is? Here is the code snippets:
1
1362
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 this be done?
1
2689
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 submit the form, the OnServerValidate method for the first pair of controls (DropDownList and TextList) fires, but for the second pair does not. It make no sense, both pairs look exactly the same to me. The only difference is that the second...
0
3537
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 OnServerValidate property of the customvalidator 3. Get reference to the Data Item object (For DataList is DataListItem, for DataGrid its DataGridItem) 4. Get reference to any controls needed for validation 5. Perform the validation
6
1395
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 tried both just the control name and the formview name appended with the control name as I find it in the rendered source. function ClientValidateCommuteMethod(ctl, args) { var txt1 = document.getElementById...
0
9712
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10341
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10089
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5530
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.