CustomValidator Problems With ValidateEmptyText


K

Kieran

Hi, am new to ASP.NET, so be gentle with me!

I have:

2 x TextBox
2 x CustomValidators
1 x ValidationSummary

Both TextBox's have AutoPostBack and CausesValidation set to True.
ValidationGroup is not set.

Both CustomValidators have EnableClientScript set to False, and
ValidateEmptyText set to True. ValidationGroup is not set.
ControlToValidate is set to the appropriate TextBox, and
onServerValidate points to the appropriate code behind function.

ValidationSummary has EnableClientScript and ShowMessageBox set to
False, and ValidationGroup is not set. ShowSummary is set to True.

The onServerValidate functions are identical, in that they set
args.IsValid to False, if args.Value.ToString().Length == 0, otherwise
args.IsValid is set to True.

There is no button to explicitly Postback. The TextBox AutoPostBack is
used for this when tabbing out of the textbox.

If I enter text into TextBox1, and tab out, then the correct error for
an empty TextBox2 is displayed. If I then enter text for TextBox2, so
both textboxes have values,and tab out, then no errors are displayed.
If I then blank out TextBox1, and tab out, an error for TextBox1 is
displayed. If I then blank out TextBox2, so that *both* textboxes are
empty, ***no error is displayed***. Huh? What am I not understanding
here? And, yes, before anyone mentions it, I am aware of the
RequiredFieldValidator, but don't wish to use it in this instance.

Thanks in advance,

Kieran

The following is the relevant designer source:

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"
CausesValidation="True"
style="z-index: 1; left: 245px; top: 65px; position:
absolute"></asp:TextBox>

<asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True"
CausesValidation="True"
style="z-index: 1; left: 245px; top: 95px; position:
absolute"></asp:TextBox>

<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="TextBox1" Display="None"
EnableClientScript="False"
ErrorMessage="TextBox1 is empty"
onservervalidate="CustomValidator1_ServerValidate"
style="z-index: 1; left: 10px; top: 65px; position: absolute;
width: 190px"
ValidateEmptyText="True"></asp:CustomValidator>

<asp:CustomValidator ID="CustomValidator2" runat="server"
ControlToValidate="TextBox2" Display="None"
EnableClientScript="False"
ErrorMessage="TextBox2 is empty"
onservervalidate="CustomValidator2_ServerValidate"
style="z-index: 1; left: 10px; top: 95px; position: absolute;
width: 190px"
ValidateEmptyText="True"></asp:CustomValidator>

<asp:ValidationSummary ID="ValidationSummary1" runat="server"
style="z-index: 1; left: 245px; top: 130px; position:
absolute; height: 38px; width: 435px"
EnableClientScript="False" />

And below are the two identical CustomValidator functions:

protected void CustomValidator1_ServerValidate(object source,
ServerValidateEventArgs args)
{
args.IsValid = (args.Value.ToString().Length == 0) ?
false : true;
}

protected void CustomValidator2_ServerValidate(object source,
ServerValidateEventArgs args)
{
args.IsValid = (args.Value.ToString().Length == 0) ?
false : true;
}
 
Ad

Advertisements

C

Cowboy \(Gregory A. Beamer\)

If you simply want to make sure the text box has something in, use a
RequiredFieldValidator. It is much simpler.
 
K

Kieran

I am aware I can use the RequiredFieldValidator for a simple 'up
front' validation for mandatory entry, but if I want something more
sophisticated, with cross-field dependencies, then I am going to have
resort to either dynamically enabling/disabling
RequiredFieldValidators, or consolidating all the validation in
CustomValidators. - It is the latter I am experimenting with, to see
whether it is possible to place everything in server-side
CustomValidators. And, based upon my experience so far, it doesn't
look like it's a reliable mechanism. I really want to know whether I
have missed something with this example code I posted. If yes, then
great, I can fix it. If I haven't missed anything, then I will happily
code server-side handling functions based on AutoPostBacks. Not a
problem either, as I've been coding such things for over 20 years. So
the question stands, is there anything wrong with the code I posted,
or is it a 'feature'?
 
Ad

Advertisements

C

Cowboy \(Gregory A. Beamer\)

When you get into cross control validation, you generally end up separating
out the client validation from the server validation, as it is very
difficult (due to incompatibilities in server and client code) to cross
check that works on both sides.

This means you write a custom validation routine for each. The client side
in JavaScript and the server side in C#. You can emit the JavaScript, if you
like keeping all control on the server, but the client side validation will
still have to check both fields, which you cannot easily set up with the
default implementation of the validators in ASP.NET.

One option, if it makes sense, is a composite control for the interdependent
controls. This only makes sense if you can get some reuse out of the
pattern, however.
 

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