Wednesday, March 28, 2012

Page_Validators in AJAX.NET problem.

Good day,

Using ASP.NET 2.0 AJAX Extensions 1.0 (1.0.61025) I am getting different output from my dev environment and another Windows 2003 server. It is causing a null reference exception because of the absence of some script, as follows.

A page that demonstrates the problem. Essentially a Textbox with a Validator in an UpdatePanel. The Textbox is hidden in a partial page update and the validator isn't removed from Page_Validators, leading to a null reference exception. Here it is.

1<%@dotnet.itags.org. Page Language="C#" %>
2<html xmlns="http://www.w3.org/1999/xhtml">
3<head>
4<title>AJAX Test</title>
5<script runat="server">
6 protected void Button1_Click(object sender, EventArgs e)
7 {
8 TextBox1.Visible = false;
9 }
10</script>
11</head>
12<body>
13 <form id="form1" runat="server">
14 <div>
15
16 <asp:ScriptManager ID="ScriptManager1" runat="server" />
17 <asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate>
18 <asp:TextBox ID="TextBox1" runat="server" />
19 <asp:RequiredFieldValidator ID="req" runat="server" ControlToValidate="TextBox1">Bad!</asp:RequiredFieldValidator>
20 <br />
21 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" />
22 </ContentTemplate></asp:UpdatePanel>
23
24 </div>
25 </form>
26</body>
27</html>
Here is the output from the dev environment that works:
1<html xmlns="http://www.w3.org/1999/xhtml">
2<head>
3<title>AJAX Test</title>
45</head>
6<body>
7 <form name="form1" method="post" action="ajaxTest.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">
8<div>
9<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
10<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
11<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTEzMDMxMDUwMDhkZDuvWiPpfDn3oBIdbrlUSDkq1FdR" />
12</div>
1314<script type="text/javascript">
15<!--
16var theForm = document.forms['form1'];
17if (!theForm) {
18 theForm = document.form1;
19}
20function __doPostBack(eventTarget, eventArgument) {
21 if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
22 theForm.__EVENTTARGET.value = eventTarget;
23 theForm.__EVENTARGUMENT.value = eventArgument;
24 theForm.submit();
25 }
26}
27// -->2829</script>
303132<script src="/WebResource.axd?d=aw9IrFUp2_oQ8Owgb8KfHw2&t=633198061661875000" type="text/javascript"></script>
333435<script src="/ScriptResource.axd?d=CKqJO5pnLiMiiKIAPSjUhlVSq_XssPmgCzQ3imV6c-ZI8ou-jtF3g7zNM6zAzFuwj0cj4E-Yc9RTfjMvluxK4g2&t=633198061661875000" type="text/javascript"></script>
36<script src="/ScriptResource.axd?d=cKz-vvqLo1ehaWyVHnXCamccZCS0TLr_WkNiTAu40bnCXSUBzVyHwrKIRYpIqnRFj0bnuLFtFlIG1uLE2IU0HlTdJFkwvyWnrxefUsmR0881&t=633278094321250000" type="text/javascript"></script>
37<script src="/ScriptResource.axd?d=cKz-vvqLo1ehaWyVHnXCamccZCS0TLr_WkNiTAu40bnCXSUBzVyHwrKIRYpIqnRFj0bnuLFtFlIG1uLE2IU0HjrmGHRUhUO2-euEh4H2gbI1&t=633278094321250000" type="text/javascript"></script>
38<script type="text/javascript">
39<!--
40function WebForm_OnSubmit() {
41if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
42return true;
43}
44// -->45</script>
4647 <div>
4849 <script type="text/javascript">
50//<![CDATA[51Sys.WebForms.PageRequestManager._initialize('ScriptManager1', document.getElementById('form1'));
52Sys.WebForms.PageRequestManager.getInstance()._updateControls(['tUpdatePanel1'], [], [], 90);
53//]]>
54</script>
5556 <div id="UpdatePanel1">
57
58 <input name="TextBox1" type="text" id="TextBox1" />
59 <span id="req" style="color:Red;visibility:hidden;">Bad!</span>
60 <br />
61 <input type="submit" name="Button1" value="" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("Button1", "", true, "", "", false, false))" id="Button1" />
62
6364</div>
6566 </div>
67
68<script type="text/javascript">
69<!--
70var Page_Validators = new Array(document.getElementById("req"));
71// -->72</script>
7374<script type="text/javascript">
75<!--
76var req = document.all ? document.all["req"] : document.getElementById("req");
77req.controltovalidate = "TextBox1";
78req.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
79req.initialvalue = "";
80// -->81</script>
8283<div>
8485<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwKV1JT3DQLs0bLrBgKM54rGBh9lq902q1zs4nZQoQSzXWrrFvC2" />
86</div>
8788<script type="text/javascript">
89<!--
90var Page_ValidationActive = false;
91if (typeof(ValidatorOnLoad) == "function") {
92 ValidatorOnLoad();
93}
9495function ValidatorOnSubmit() {
96 if (Page_ValidationActive) {
97 return ValidatorCommonOnSubmit();
98 }
99 else {
100 return true;
101 }
102}
103// -->104</script>
105
106<script type="text/javascript">
107<!--
108Sys.Application.initialize();
109110document.getElementById('req').dispose = function() {
111 Array.remove(Page_Validators, document.getElementById('req'));
112}
113// -->114</script>
115</form>
116</body>
117</html>
118

Here is output from a different server that fails.

1<html xmlns="http://www.w3.org/1999/xhtml">2<head>3<title>AJAX Test</title>45</head>6<body>7 <form name="form1" method="post" action="ajaxTest.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">8<div>9<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />10<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />11<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTEzMDMxMDUwMDhkZBFseLlnE6oRW+2WytSmHIxuhR3k" />12</div>1314<script type="text/javascript">15<!--16var theForm = document.forms['form1'];17if (!theForm) {18 theForm = document.form1;19}20function __doPostBack(eventTarget, eventArgument) {21 if (!theForm.onsubmit || (theForm.onsubmit() != false)) {22 theForm.__EVENTTARGET.value = eventTarget;23 theForm.__EVENTARGUMENT.value = eventArgument;24 theForm.submit();25 }26}27// -->2829</script>303132<script src="/WebResource.axd?d=y79a8WX0nIrEh3fkGSsX9A2&t=633281399937474336" type="text/javascript"></script>333435<script src="/WebResource.axd?d=w26mxAmgRXp-rT77oPfALL7gzYUT-X6oWLHzd_D6LjY1&t=633281399937474336" type="text/javascript"></script>36<script src="/ScriptResource.axd?d=wZTJCSpVLa8cXWFJgmiuXX-hnAsgClz1-CgGif5xa_-p3CzrOtmsIfh3Y28aQcYFVC5wnr2IJhBHoh2c9Zc0Ch0RynufR4TLZTewW0cBhsc1&t=633281590101841744" type="text/javascript"></script>37<script src="/ScriptResource.axd?d=wZTJCSpVLa8cXWFJgmiuXX-hnAsgClz1-CgGif5xa_-p3CzrOtmsIfh3Y28aQcYFVC5wnr2IJhBHoh2c9Zc0CrgGo9Nt84M88yWLG3iczXI1&t=633281590101841744" type="text/javascript"></script>38<script type="text/javascript">39<!--40function WebForm_OnSubmit() {41if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;42return true;43}44// -->45</script>4647 <div>4849 <script type="text/javascript">50//<![CDATA[51Sys.WebForms.PageRequestManager._initialize('ScriptManager1', document.getElementById('form1'));52Sys.WebForms.PageRequestManager.getInstance()._updateControls(['tUpdatePanel1'], [], [], 90);53//]]>54</script>5556 <div id="UpdatePanel1">5758 <input name="TextBox1" type="text" id="TextBox1" />59 <span id="req" style="color:Red;visibility:hidden;">Bad!</span>60 <br />61 <input type="submit" name="Button1" value="" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("Button1", "", true, "", "", false, false))" id="Button1" />626364</div>6566 </div>6768<script type="text/javascript">69<!--70var Page_Validators = new Array(document.getElementById("req"));71// -->72</script>7374<script type="text/javascript">75<!--76var req = document.all ? document.all["req"] : document.getElementById("req");77req.controltovalidate = "TextBox1";78req.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";79req.initialvalue = "";80// -->81</script>8283<div>8485<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwKvmrnGBwLs0bLrBgKM54rGBh5nv4YQrwGF0j7OWce2dFyfGsmj" />86</div>8788<script type="text/javascript">89<!--90var Page_ValidationActive = false;91if (typeof(ValidatorOnLoad) == "function") {92 ValidatorOnLoad();93}9495function ValidatorOnSubmit() {96 if (Page_ValidationActive) {97 return ValidatorCommonOnSubmit();98 }99 else {100 return true;101 }102}103// -->104</script>105106<script type="text/javascript">107<!--108Sys.Application.initialize();109// -->110</script>111</form>112</body>113</html>114
The working machine is a dev machine with a long history of messing around.The failing machine is a freshly installed Windows 2003 install with only critical updates, .Net Framework 2.0, Ajax Extensions.I uninstalled and reinstalled Ajax Extensions on the dev machine using the same msi.
I am unable to figure out what is different in the configuration.

Any suggestions would be tremendous!

Thanks,

Craig


Validation controls, which includes theBaseCompareValidator,BaseValidator,CompareValidator,CustomValidator,RangeValidator,RegularExpressionValidator,RequiredFieldValidator, andValidationSummary control are not compatible with UpdatePanel

See it here

http://weblogs.asp.net/scottgu/archive/2007/01/25/links-to-asp-net-ajax-1-0-resources-and-answers-to-some-common-questions.aspx

You can downlaod compatible ersion of validators from here

http://blogs.msdn.com/mattgi/archive/2007/01/23/asp-net-ajax-validators.aspx


Thanks muchly! :)

No comments:

Post a Comment