473,387 Members | 1,364 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Using CustomValidator to report unexpected errors

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
2 2386
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
7
by: Tom | last post by:
I know how to use these great controls. They work very well. But is there a method or property I can use to set the focus to the field that is in error. For example... If I have a page with 5...
2
by: George Ter-Saakov | last post by:
Hi. I am fighting the CustomValidator strange behavior for second day already. I created some test project which is only to reproduce the problem. 1. I created UserControl which has an only a...
1
by: Tom | last post by:
Hi I need to validate the duplication of username. Here is the aspx code [code <td>User Name:</td><td><asp:textbox id="username" Runat="server"></asp:textbox><asp:requiredfieldvalidator...
1
by: SMG | last post by:
Hi All. My forms has two textboxes, 1 username, 2 password. Both has requiredfield validator it works fine when there is no input in these textboxes. And the errorMsg is shown in...
2
by: Barbara Alderton | last post by:
I setup some standard Required Field Validation controls and one Custom validation control on an ASP.NET page (within a user control) to validate text entry. I also setup a Summary Control to post...
0
by: Keith | last post by:
I have a web form that contains a repeater control that is designed to ask like a check book register. Clicking on a certain transaction takes the user to a different .aspx page where it can be...
0
by: Keith | last post by:
I have a web form that contains a repeater control that is designed to ask like a check book register. Clicking on a certain transaction takes the user to a different .aspx page where it can be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.