asp.net validation control causing "random" error in javascript
I have an asp:FormView
on a control (in an ascx file) which is loaded onto a portal page along with some other controls.
I need to add validation to the form, but when I add any of the validation controls I get an apparently random error in the generated JavaScript when the page is loaded, and the validation doesn't fire when I need it to.
This is the code that I've got:
<asp:TextBox ID="FPITextBox4" runat="server" Text='<%# Bind("SomeNumber") %>'></asp:TextBox>
<asp:RangeValidator ID="RangeValidator4" runat="server" ControlToValidate="FPITextBox4" Text="*"
ErrorMessage="The number must be开发者_StackOverflow社区 a whole number between 0 and 100,000" Type="Integer"
MaximumValue="100000" MinimumValue="0" ToolTip="Must be between 0 and 100,000"></asp:RangeValidator>
This is the error I get when the page is loaded:
Message: Expected ';'
Line: 1159
Char: 60
Code: 0
When I look at line 1159 in the generated code it looks like this:
var ctl00_ctl00_main_col2_control_0_widgetcontrolX_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_widgetcontrol_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_FormView1_RangeValidator4 = document.all ?
document.all["ctl00_ctl00_main_col2_control_0_widgetcontrolX_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_widgetcontrol_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_FormView1_RangeValidator4"] :
document.getElementById("ctl00_ctl00_main_col2_control_0_widgetcontrolX_3eba4918-7c7d-
47aa-a089-fd0ead0609fe_widgetcontrol_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_FormView1_RangeValidator4");
The code immediately above this is:
<script type="text/javascript">
//<![CDATA[
var Page_Validators = new Array(document.getElementById(
"ctl00_ctl00_main_col2_control_0_widgetcontrolX_3eba4918-7c7d
-47aa-a089-fd0ead0609fe_widgetcontrol_3eba4918-7c7d-47aa-a089-
fd0ead0609fe_FormView1_RangeValidator4"));
//]]>
</script>
The same code works when the control is just instantiated onto a test page all by itself.
It appears that the error only happens in IE 8. I've tested the same page in Firefox 3.6 and it all works as expected.
Correction
It appears that the validation is working. I had a ValidationSummary
control set to show a message box and that wasn't appearing which led me to believe that the validation wasn't firing. When I changed that to ShowSummary="true" ShowMessageBox="false"
the error message did appear and the data isn't saved.
So all it means now is that I've got a random script error that (at the moment) doesn't appear to be affecting the workings of the page.
However, I would still like to remove the error - just in case it's hiding something else.
you really found a bug in .Net client generation code - have a look at http://haacked.com/archive/2006/07/14/ASP.NET2.0ClientValidationJavascriptBug.aspx
But no resolution from M$ - you'll have to generate IDs in a different way...
I've found that this type of error results because of an error in one of my scripts, where there is a missing ;. Do you have JS code in your page and is all of that correct, no missing semi-colons?
HTH.
精彩评论