By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,046 Members | 2,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,046 IT Pros & Developers. It's quick & easy.

Using CustomValidator to report unexpected errors

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Stephen Miller wrote:
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

Nov 18 '05 #2

P: n/a
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: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Stephen Miller" <js******@hotmail.com> wrote in message
news:cd**************************@posting.google.c om...
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

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.