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