Validator fails after enabling

T

Tumurbaatar S.

I'm trying to dynamically hide/show a HTML table server control.
My form contains one CheckBox control and its AutoPostBack set to true.
The Checked value of this box controls whether to show or hide the table.
Initially this CheckBox is not checked, so the table and all web control it
contains
is not rendered to a client. The table contains several TextBox controls and
each TextBox has a pair of RequiredFieldValidator and CompareValidator.
When I click the previous checkbox, the form postbacks and the server side
code sets HtmlTable.Visible to true. Now all TextBoxes appear at the client
side,
BUT ALL validators indicate failure by showing theirs Text message. Why?
When I do not hide/show the table and it is always visible, the validators
work normally.
This only happens when the table and its content (the textboxes and
validators)
are dynamically showed/rendered. As I know, the CheckBox's AutoPostBack
does not trigger a validation process. Any ideas?
I tried to trace my page and set several breakpoints at Page_Load, PreRender
and
CheckBox_CheckedChanged. At these points I checked values of the IsValid
properties
of the validators. They were always TRUE. I wonder when they switches to
FALSE?
If I'm right, a validator displays the Text only when IsValid is false. Yes?
 
T

Tumurbaatar S.

I'm sorry. I did not clearly explain the scenario.
There's a HtmlTable with TextBoxes and Validators (Required and Compare).
When the table is visible (I show it by "HtmlTable.Visible = true") and ALL
its rows (where textboxes and validators) are visible too, the validators
work
normally. When I show the table, but hide SOME rows
(HtmlTable.Rows.Visible = false),
then the validators located within visible rows work ok. But in next
roundtrip
I show previously hidden rows (HtmlTable.Rows.Visible = true). And the
validators
that were hidden in prior step, appear as FAILED: theirs error texts are
shown.
Though these validators works normally (properly detects blank or invalid
values),
only they initially appears with visible errors. The validators that were
visible already
are not have any problem.
I've done here some more tests and found that is not related to a validation
process.
Even the validators are disabled (Enabled = false), they re-appears with
visible errors
texts.
I found some workaround, but it does not really solve the problem, only
makes
the problem hidden.
Instead of resetting HtmlTable.Rows.Visible properties, I apply CSS
attribute "display" with "none" or "block" values to the rows. In this case,
the hidden rows and controls are rendered to a client, only they are
invisible.
So I need to disable the validators before.
Below is my new method to hide/show rows:

private void ToggleRows(bool Visible)
{
//this is an array of textboxes and validators located in hidden
rows
WebControl[] ctrls =
{
Trans20Txt, Trans20Val1, Trans20Val2,
Trans40Txt, Trans40Val1, Trans40Val2,
Trans00Txt, Trans00Val1, Trans00Val2
};
string display = Visible? "block" : "none";

for (int i = 0; i < ctrls.Length; i++)
ctrls.Enabled = Visible;
for (int i = 3; i < FormTbl.Rows.Count - 1; i++)
FormTbl.Rows.Style["display"] = display;
}

My old method was shorter and simpler:

private void ToggleRows(bool Visible) // no reason to disable validators
{
for (int i = 3; i < FormTbl.Rows.Count - 1; i++)
FormTbl.Rows.Visible = Visible;
}



P.S. The checkbox I mentioned in prior post, I use to as a trigger of
visibility of the table.
 
P

Peter Blum

When a Validator's IsValid property is false at the time of Render, it
writes out its error message. A call to Page.Validate() will update IsValid
on each validator. That call should happen before OnPreRender, so if IsValid
is true while debugging PreRender, we must look for another cause.

When validators output themselves, they are always a <span> tag containing
the error message. To keep them initially hidden, they include
style="visibility:hidden" (when Display="Static") or
style="visibility:hidden;display:none" (when Display="Dynamic"). I am
wondering if your HTML includes this style or not. In any case, look
carefully at the HTML generated to try to locate the source of it becoming
visible. There may be a style sheet problem.


--- 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

Top