Using CustomValidator to report unexpected errors

S

Stephen Miller

Can the CustomValidator be used to simply report unexpected errors,
without requiring Client/Server validation? To explain, say you had a
simple text box and button that did a Full-text Search of a Catalogue
in SQL. Malformed search requests can generate a variety of errors,
which I simply want to catch, set the CustomValidator's IsValid method
to false and assign the ErrorMessage to the CustomValidator's Message
property. For example:

<asp:TextBox id="txtSearch" runat="server"></asp:TextBox>
<asp:CustomValidator id="valSearch" runat="server"
ControlToValidate="txtSearch" ErrorMessage="Unexpected Error"
EnableClientScript="false">*</asp:CustomValidator>
<asp:Button id="cmdSearch" runat="server" Text="Search"></asp:Button>
<asp:ValidationSummary id="valSummary"
runat="server"></asp:ValidationSummary>

and in code behind:

Try
grdResults.DataSource = doSearch(txtSearch.Text).DefaultView
grdResults.DataBind()
Catch ex As Exception
valSearch.IsValid = False
valSearch.ErrorMessage = ex.Message
End Try

When I run this I get nothing! The literal valSearch is not rendered
on the page and the ValidationSummary has no content.

I have set EnableClientScript to False, because I don't want the
CustomValidator to do any actual validation, simple highlight that the
TextBox requires attention and report the error message in the
Summary. Is this a reasonable thing to do? If so, how should I
approach the problem?

Thanks,

Stephen
 
S

Stefano Mostarda

Hi Stephen,

From what I know, you can't programmaticaly set the validation controls
property IsValid to true. Try to use a simple label control.

HTH,

Stefano Mostarda MCP
Rome Italy
 
P

Peter Blum

The CustomValidator is designed to take a server side event handler to
validate. I suspect that it acts like its disabled when there is no such
event hooked up. You will have to move the code inside such an event
handler. Its concievable but tricky to set things up so that on post back,
you run your search code, set a flag and then know that the custom
validator's event handler will run after the search code. But here's how I'd
do this:

- Turn off client-side validation on submit by setting the submit button's
CausesValidation property to false. This will also prevent the server side
from running validation automatically.
- In your post back event handler for the button click, do the necessary
code that does the search and sets a boolean property to true or false on
your page class.
- In that event handler, also call Page.Validate() after the search code has
done its stuff. Now you have ordered the call to the custom validator's
event handler after running the search.

FYI: In writing my product "Professional Validation And More", I noticed
that the model you present was lacking in MS validation and desirable. If
you use my product, you can use the "IgnoreConditionValidator" to do what
you described: just set the error message and IsValid property right in the
middle of your other logic. http://www.peterblum.com/vam/home.aspx.

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top