CustomValidator problems.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I've spent the last day or so debugging a problem with a CustomValidator and
am now totally stumped! Basically I use a number of CustomValidator's on my
page, but have cut this down now to only 2. When the page is first loaded the
CustomValidator are made and the generated HTML contains both of them. When
the "Apply" button is clicked the page is posted back and my database is
updated. The page is then rebuilt and sent back to the browser. During this
second rebuild, since the data on the page was changed by the user, only 1 of
the CustomValidator's is needed and therefore not constructed and not added.
The curious thing is that after the page is reloaded, if you look at the
html, it contains a definition for both of the CustomValidator's. I've even
had situations where if only one is in the html, it has the ID of the first
and the ErrorMessage & ClientValidationFunction of the second so appears as
a mixture of the two.

This really is very odd and I am hoping that somebody out there might be
able to help.

Thanks in advance.
 
Hello,

Can you post some code to show how the CustomValidator are made and how one
of them are removed after postback? This may help us better undrstand the
problem and find what is going on there.

Regards,

Luke
 
Hello Luke,

Thanks for the reply and yes fair comment. Below is a synopsis of the code.
One thing to note from your comment that I guess might be the cause of my
problem is that I NEVER remove the customValidator during a postback, I just
dont re-add it during a postback.

To clarify the situation, given the code below, here is what happens.

1) Initial call to OnInit(), dataConfidence is less than
field.ConfidenceThreshold, so ConfigureConfidenceLevelValidation() is called
and the CustomValidator made and added to the scriptPlaceHolder (a
System.Web.UI.WebControls.PlaceHolder). The customValidator appears correctly
oin the html.

2) After the "Apply" button is clicked the page is submitted and IsPostBack
== true. The call to UserCorrectedLowConfidenceField() returns true and
dataConfidence is set to 100. This means that dataConfidence is NOT less than
field.ConfidenceThreshold so ConfigureConfidenceLevelValidation() is NOT
called but the validator still appears in the html when you view the source
on the newly generated page.

Thanks again for your post I appreciate your help.

Regards
Christian

-----------------------------------------------------
Basically the code looks like this:

OnInit()
{
....
.... // Various bit of unrelated code.
....
....

if (IsPostBack)
{
if (UserCorrectedLowConfidenceField(field))
{
dataConfidence = 100;
}
dataValue = Request.Form[field.FieldName];
}

tb = AddTextBox(cell, field, dataValue);

if (dataConfidence < field.ConfidenceThreshold)
{
ConfigureConfidenceLevelValidation(tb, field, dataConfidence);
}
....
.... // Various bit of unrelated code.
....
....

}


private void ConfigureConfidenceLevelValidation(Control tb,
FormFieldsDS.FormFieldsRow field, double dataConfidence)
{
CustomValidator cv = new CustomValidator();
this.Validators.Add(cv);
scriptPlaceHolder.Controls.Add(cv);
cv.ControlToValidate = tb.ID;
cv.ErrorMessage = string.Format("{0}: field has a low confidence level.",
field.FieldName);
cv.Display = ValidatorDisplay.None;
cv.ClientValidationFunction = "confidenceLevelValidation";
}
 
I think you may add a breakpoint in ConfigureConfidenceLevelValidation to
see how many time it was called after postback, I suspect it was called
twice incorrectly and use wrong parameters so that there still two
validator in the HTML code.

Luke
 
Hi Luke

Thanks again for the response. Before posting this question I spent over a
day debugging this problem and what I noticed is that the function
ConfigureConfidenceLevelValidation () is only called once and the net result
is 2 validators. This is why I am so confused and can see no way that there
should be 2. Do you know if you need to remove these validators? perhaps they
are rebuilt on postback from the viewstate and you have ot remove them.
Saying that, I have had a look at the value of "this.Validators.Count" at the
beginning of OnInit() during the postback and it was zero and at the end of
OnInit() it was one, again odd that 2 appear in the html.
 
Hello,

CustomValidator control has a property named "EnableViewState", you may set
it to False in your code (ConfigureConfidenceLevelValidation) to see if it
can help.

Luke
 
Back
Top