Usually there is the need to do some sort of validation across more than one control on a web form. In my case I have created a simple set of text boxes. txtnum1, txtnum2 and txtresult. The requirement is that txtnum1 + txtnum2 must equal txtresult. This is common on financial applications or web based financial helpers. My 401K web admin tool comes to mind. My allocations into each mutual fund must equal 100%.
So I created the text boxes, and grabbed a customer validator and placed it on the form. I modified the error message and left everything else the same. Actually I also adjusted the size of the text boxes as well. I have this on a form that already is being submitted by another control, so I dont need to add a button or linkbutton- but you might.
After adding the controls to the page I double click on the customer validator. This opens the code behind page which has the default method for the control already added for me:
Private Sub CustomValidator1_ServerValidate(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
Within this function I can add whatever code I need to validate, and I to ensure the page isvalid or not I just modify the args.isvalid property to true or false:
If txtNum1.Text.Length > 0 _
AndAlso txtNum2.Text.Length > 0 _
AndAlso txtResult.Text.Length > 0 Then
'give the validation a shot
If CType(txtNum1.Text, Double) + CType(txtNum2.Text, Double) <> CType(txtResult.Text, Double) Then
args.IsValid = False
Else
args.IsValid = True
End If
End If
Now if you check page.isvalid at any point you will see if this validated or not. This is important if you have a need for the values to be the same such as updating a database or something like that.
Page.Validate()
lblFiles.Text &= Page.IsValid
This should be easy, but let me know.
Trackback • Posted by Jason in Uncategorized category
Please leave a reply...