Recently, I was trying to modify an existing aspx page when client-side
validation on that page stopped working. I searched this group and the web
in general and found that other people have had the same issue. However,
none of the suggested fixes solved my particular problem. I tracked down the
cause of the problem, which is related to aspx page parser's handling of
controls inside html comments. The problem may be quite common and well
known. However, since I was not able to find an explanation anywhere, I am
going post my findings in case others run into the same issue.
The problem is related to how aspx page parser translates apsx controls
inside html comments. For example, the following code
<!--
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
-->
generates the following html
<!--
<input name="TextBox1" type="text" id="TextBox1" />
-->
Essentially, the parser ignores the comment and renders the control in html.
This presents no problem because the rendered html is enclosed inside html
comment and will be ignored by the browser. However, if there is a
validation control inside a html comment, such as the code below,
<!--
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="rfvTextBox1" runat="server"
ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
-->
a problem occurs. The parser translates this comment into
<!--
<input name="TextBox1" type="text" id="TextBox1" />
<span id="rfvTextBox1" controltovalidate="TextBox1"
evaluationfunction="RequiredFieldValidatorEvaluate IsValid" initialvalue=""
style="color:Red;visibility:hidden;"></span>
-->
The parser also adds the following javascript to the rendered page
<script language="javascript" type="text/javascript">
<!--
var Page_Validators = new Array(document.all["rfvFName"],
document.all["rfvLName"], document.all["rfvEmail"],
document.all["rfvTextBox1"]);
// -->
</script>
In addition to the required field validator shown above, there are several
other validation controls on my page and all of them are listed in the array
Page_Validators. The issue is that document.all["rfvTextBox1"] always
returns null because it is enclosed inside html comment. The problem hits
home when client-side validation is initialized during a page load. Below is
the initialization function, copied exactly from WebUIValidation.js,
function ValidatorOnLoad() {
if (typeof(Page_Validators) == "undefined")
return;
var i, val;
for (i = 0; i < Page_Validators.length; i++) {
val = Page_Validators[i];
if (typeof(val.evaluationfunction) == "string") {
eval("val.evaluationfunction = " + val.evaluationfunction +
";");
}
if (typeof(val.isvalid) == "string") {
if (val.isvalid == "False") {
val.isvalid = false;
Page_IsValid = false;
}
else {
val.isvalid = true;
}
} else {
val.isvalid = true;
}
if (typeof(val.enabled) == "string") {
val.enabled = (val.enabled != "False");
}
ValidatorHookupControlID(val.controltovalidate, val);
ValidatorHookupControlID(val.controlhookup, val);
}
Page_ValidationActive = true;
}
This function hooks up each validators defined in the Page_Validators array.
However, as in the example given above, the last validator is null and the
code does not guard against nulls! This results in an exception, and the
variable Page_ValidationActive is never set to true, effectively turning off
client-side validation.
Hong Hao 1 3914
> Recently, I was trying to modify an existing aspx page when client-side validation on that page stopped working. I searched this group and the web in general and found that other people have had the same issue. However, none of the suggested fixes solved my particular problem. I tracked down the cause of the problem, which is related to aspx page parser's handling of controls inside html comments. The problem may be quite common and well known. However, since I was not able to find an explanation anywhere, I am going post my findings in case others run into the same issue.
The problem is related to how aspx page parser translates apsx controls inside html comments. For example, the following code
<!-- <asp:TextBox id="TextBox1" runat="server"></asp:TextBox> -->
generates the following html
<!-- <input name="TextBox1" type="text" id="TextBox1" /> -->
Essentially, the parser ignores the comment and renders the control in html. This presents no problem because the rendered html is enclosed inside html comment and will be ignored by the browser. However, if there is a validation control inside a html comment, such as the code below,
<!-- <asp:TextBox id="TextBox1" runat="server"></asp:TextBox> <asp:RequiredFieldValidator id="rfvTextBox1" runat="server" ControlToValidate="TextBox1"></asp:RequiredFieldValidator> -->
a problem occurs. The parser translates this comment into
<!-- <input name="TextBox1" type="text" id="TextBox1" /> <span id="rfvTextBox1" controltovalidate="TextBox1" evaluationfunction="RequiredFieldValidatorEvaluate IsValid" initialvalue="" style="color:Red;visibility:hidden;"></span> -->
The parser also adds the following javascript to the rendered page
<script language="javascript" type="text/javascript"> <!-- var Page_Validators = new Array(document.all["rfvFName"], document.all["rfvLName"], document.all["rfvEmail"], document.all["rfvTextBox1"]); // --> </script>
In addition to the required field validator shown above, there are several other validation controls on my page and all of them are listed in the array Page_Validators. The issue is that document.all["rfvTextBox1"] always returns null because it is enclosed inside html comment. The problem hits home when client-side validation is initialized during a page load. Below is the initialization function, copied exactly from WebUIValidation.js,
function ValidatorOnLoad() { if (typeof(Page_Validators) == "undefined") return; var i, val; for (i = 0; i < Page_Validators.length; i++) { val = Page_Validators[i]; if (typeof(val.evaluationfunction) == "string") { eval("val.evaluationfunction = " + val.evaluationfunction + ";"); } if (typeof(val.isvalid) == "string") { if (val.isvalid == "False") { val.isvalid = false; Page_IsValid = false; } else { val.isvalid = true; } } else { val.isvalid = true; } if (typeof(val.enabled) == "string") { val.enabled = (val.enabled != "False"); } ValidatorHookupControlID(val.controltovalidate, val); ValidatorHookupControlID(val.controlhookup, val); } Page_ValidationActive = true; }
This function hooks up each validators defined in the Page_Validators array. However, as in the example given above, the last validator is null and the code does not guard against nulls! This results in an exception, and the variable Page_ValidationActive is never set to true, effectively turning off client-side validation.
Hong Hao
True, the parser looks just for "runat=server" controls and ignores
client-side comment brackets. This might be by design: you could put
a Label there to render client-side comments.
Unfortunately this has the side-effect that you noticed.
A couple of ways around it:
- use server-side comments: <%!-- --%>
- remove the "runat=server" (I usually "destroy" it by adding an "X":
runXat=server is not recognised and ignored)
Hans Kesting This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Michael Rybak |
last post by:
hi, everyone.
I'm writing a 2-players game that should support network mode. I'm now
testing it on 1 PC since I don't have 2. I directly use sockets, and
both client and server do...
|
by: Raquel |
last post by:
How do I know whether the 'runtime client' and the 'application
development client' are installed on my machine? When I issue the
command "db2licm -l", it gives the following output:
Product...
|
by: Rhino |
last post by:
I am trying to verify that I correctly understand something I saw in the DB2
Information Center.
I am running DB2 Personal Edition V8.2.1 on Windows. I came across the
following in the Info...
|
by: whidy |
last post by:
|
by: J Huntley Palmer |
last post by:
I am having a horrific time integrating uw-imap's c-client for imap
support in php. The problem is a whole bunch of "Text relocation remains
referenced against symbol" errors during
linking....
|
by: khu84 |
last post by:
Here is client server very simple code, seems to work with telnet but with with web client code gives blank output.
Following is the server code:-
<?php
function...
|
by: coolsidsin |
last post by:
I have a apsx form (Form A), which on submiting does a time consuming task. So i want to open a new aspx window (Form B) in which I want do do that time consuming task and show progress to the user...
|
by: nsaffary |
last post by:
hi
I hava a client/server program that run correctly when i run it in one computer(local) but when I run client on a one computer and run server run on another, connection does not stablish.(I set...
|
by: abdunnabisk |
last post by:
Any body has any idea on How to call apsx.cs mthod(C#) from javascript?
Thanks
Abdun Nabi Sk
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
| |