Hidden validators cause javascript error

I

Ian Cox

I have a web form that contains a Datagrid. This grid has a number of
columns, one of which contains a text box and validator for that text box.
Everything works fine, when I press the "Save" button the validator works as
expected...however...

....I want to hide certain columns under certain conditions. One of the
columns I wish to hide is the column that contains the text box and the
validator. This is done by something like

grdAddItem.Columns[5].Visible = false;

in the code behind.

Now the page renders fine, but when I press the Save button I get a
javascript error "Page_Validators is undefined". The validator is contained
within the form and the form does have runat=server set.

Am I missing something? or is this a bug?

I have found a workaround for this problem which is to add another validator
to the form together with a text box for it it to validate and make them
both invisble and set up so that the new validator always validates. If I do
this I no longer get the javascript error.
e.g.
<asp:TextBox ID=txtTest Runat=server Visible=False>1</asp:TextBox>
<asp:CompareValidator ID="Comparevalidator3" Type="Integer"
ControlToValidate="txtTest" ErrorMessage=""
Runat="server" Operator="DataTypeCheck" CssClass="errorMessage"
Display="None" ValueToCompare="1"></asp:CompareValidator>

Any ideas?

Cheers
Ian
 
S

Steven Cheng[MSFT]

Hi Lan,

From your description, you have a datagrid which contains a textbox and a
validation control in a certain column. You found that there will occur
script error("Page_validators undefined") when you hidden that column and
click a certain button(causevalidtion) to submit the page, yes?

As for the problem, I've also done some simple tests and did see the this
behavior. I think the problem is because normally the webform will generate
the following script code (enable client side validation) when there is
validators on the page and use client side valiation:

<script language="javascript" type="text/javascript">
<!--
var Page_Validators = new Array(document.all["dgMain__ctl2_rfvItem"],
document.all["dgMain__ctl3_rfvItem"], document.all["dgMain__ctl4_rfvItem"],
document.all["dgMain__ctl5_rfvItem"], document.all["dgMain__ctl6_rfvItem"]);
// -->
</script>


the code create an Array that hold all the validtor elements on page.
However, when we hidden the column( which means we hidden those
validators), the above code disappeared. But since our asp.net
submit Button is "causeValidtion=true" , so when we hit the button, the
clientside script will still looking for the

"Page_Validtors" variable but failed to find it which lead to the error you
encountered. So I suggest that we turn off the button's causeValidation
when we hidden the validators on the page since no validation is needed at
that time. How do you think?

If there is anything unclear, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
I

Ian Cox

Thanks Steven. That makes sense to me, and the suggested solution works.

Thanks for your help

Steven Cheng said:
Hi Lan,

From your description, you have a datagrid which contains a textbox and a
validation control in a certain column. You found that there will occur
script error("Page_validators undefined") when you hidden that column and
click a certain button(causevalidtion) to submit the page, yes?

As for the problem, I've also done some simple tests and did see the this
behavior. I think the problem is because normally the webform will generate
the following script code (enable client side validation) when there is
validators on the page and use client side valiation:

<script language="javascript" type="text/javascript">
<!--
var Page_Validators = new Array(document.all["dgMain__ctl2_rfvItem"],
document.all["dgMain__ctl3_rfvItem"], document.all["dgMain__ctl4_rfvItem"],
document.all["dgMain__ctl5_rfvItem"],
document.all["dgMain__ctl6_rfvItem"]);
// -->
</script>


the code create an Array that hold all the validtor elements on page.
However, when we hidden the column( which means we hidden those
validators), the above code disappeared. But since our asp.net
submit Button is "causeValidtion=true" , so when we hit the button, the
clientside script will still looking for the

"Page_Validtors" variable but failed to find it which lead to the error you
encountered. So I suggest that we turn off the button's causeValidation
when we hidden the validators on the page since no validation is needed at
that time. How do you think?

If there is anything unclear, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
P

Peter Blum

There is a bug in the DataGrid that I confirmed with Microsoft about a year
ago. When a control that outputs javascript is contained in a header, footer
or column that is hidden, the javascript is still output, but the HTML is
not. In this case, the validator outputs itself into the Page_Validators
array. When that array is created, it expects to convert
document.all[validatorid] into an object. It does not. The validator scripts
which iterate through Page_Validators are not expecting a null in
Page_Validators and invoke the error.

Solution: any control that outputs javascript should explicitly be set to
Visible=false when the header, footer, or column containing it is hidden.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top