TextBox Custom Validation?

Netnoobie

Well-known member
Joined
Feb 6, 2003
Messages
94
Location
Philadelphia
Hello all. Im trying to (what I think is simple, maybe Im wrong) do a validation when the user tabs off of a textbox. This validation is a database check to make sure that the entered text is valid based on internal criteria.

I cant see to get into custom validator code while running it, and for some reason what I did just doesnt look and feel correct. How could I call up some sort of validator when the textbox is tabbed off of? And also validators retun boolean values right? Like IsValid = True? I need to use server side validation because of the database, but I cant seem to link the tabbing off of the textbox with firing the validation event...then marking the page invalid then and showing it. Im using this method for an editeable datagrid as well as regular text boxes on a form. This means Im really needing be able to fire everything without a submit button as I seen in examples everywhere.

Ive never really played with custom validators before and Im felling really lost.

Thanks!
Bryan
 
I could be wrong, but as far as I know you cant do server side validation until the user actually clicks a post back (or something where he sends info to the server). The best you can do is client side validation, and that requires Javascript (which I believe the ASP.NET validation controls use if you set them up to do so).
 
You could set the AutoPostBack property of the textboxes to True - but this would cause a server round trip every time you tab between textboxes.
 
Hm.. thats a good choice. Isnt there also an option for the latest IE browsers where you can redisplay info. as if the browser didnt refresh?
 
Strange CustomValidator

You can make serverside validation with CustomValidator control, but initially, I found the same pb as you, about CustomValidator: no events fired. Finally, it works if i put :

ControlToValidate=""

instead of a specific control. In this way, you can use the method pointed on in OnServerValidate to test the control you want.

Bur I am still interested if you make it work with a nonzero string in ControlToValidate.

Here is the code I used:
<asp:CustomValidator id="devisValidator" runat="server" ControlToValidate="" ErrorMessage="Estimate needed" OnServerValidate="devisValidator_ServerValidate">*</asp:CustomValidator>

and the servervalidate method, with a database access :

Public Sub devisValidator_ServerValidate(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles devisValidator.ServerValidate
Dim iEtat = DsEvosDetail1.Tables(0).Rows(0).Item("ETAT")
If iEtat = Status.Ouverte Then
If lblDevisData.Text = "" Then
args.IsValid = False
Else
args.IsValid = True
End If
End If
End Sub
 
If you want to keep the client from reloading the active page but want to retrieve data from a server, I have done it like this in the past.

If the site is framed, have one frame which is of zero (0) size so it does not display. Have you custom validator call a page which retrieves your data as javascript variables or arrays into this frame. You can use this frame to store quite a bit of data and it is easily reloadable and accessable.

If the site is not framed use the same process but open a new browser window off screen so the user cannot see it. In this case you should close the window as soon as your done as it is a bad look to leave these windows open when your application is finished with it.

The only problem with this method is timing, you may need to have the page loading in the frame or new window call a method in the primary window when it is done. Or you can poll the frame or window for an expected variable and when it appears you know all the real data is available.

Its a messy way of doing it but it makes for a clean user interface and it look realy professional.



:-\ :-\
 
Back
Top