TutorialsRoom.com, Where knowledge lands
RSS Feed

Subscribe: RSS or e-mail

Home » Web Development » ASP.NET »

ASP.Net Validation Controls Part 3

bitmap_vector (1K)
In the last part of the ASP.Net validation controls tutorial, we will learn about the ASP.Net Custom Validation Control and the ASP.Net Validation Summery Control and what they are used for.

What if your validation criteria is more complicated than what we have seen in the previous parts of the validation controls tutorial? What if your validation criteria depends on a combination of multiple controls values not a single control? Then You will need the ASP.Net Custom Validation Control which -as the name implies- depends on the custom code you write. Because you have to write the code for validation yourself -not like the other validation controls- the validation occurs mainly on the server side. if you want the validation to occur on the client side also, you will need to provide your own code in a scripting language browsers could understand such as JavaScript. Also, tell ASP.Net Custom Validation Control about this function using the ClientValidationFunction property. Let's explore how the ASP.Net Custom Validation Control Works.

Drop a CheckBox control and name it chkName, a TextBox control and name it txtName, a CustomValidation control and name it cvName, a Button control and name it btnSubmit and modify the ErrorMessage and the Text properties of the controls so your page looks like that:

ASP.Net Validation Controls

Not like the other ASP.Net Validation Controls, you don't have to set the ControlToValidate property of the Custom Validation Control. So, suppose we want to force our users to enter their names only if they check the chkName CheckBox so our validation depends on two controls values, of course you can make your code much more complicated than that but for the purpose of this tutorial, we will keep it simple. Here is the code that we will be using for our Custom Validator ServerValidate event:

 protected void cvName_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (chkName.Checked && txtName.Text == "")
        {
            args.IsValid = false;
        }
    } 

First, we check if the chkName is checked and txtName.Text is empty because we don't want our validation to occur if the chkName is not checked. Only then, we will set the args.IsValid to false. args.IsValid is what you will be using most of the time when you are using the ASP.Net Custom Validator. The whole idea is to set args.IsValid to true or false based on whatever checks you want. If you set the args.IsValid to false, the text in the ErrorMessage property is displayed and the Page.IsValid is set to false, you can check the Page.IsValid anywhere in your page, we will use this check in the Click event of the btnSubmit:

 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid && chkName.Checked)
        {
            Response.Write("Your name is: " + txtName.Text);    
        }
    }

We are using the Response.Write to write the name that the user entered only if the Page.IsValid is true and the chkName is checked.

The last control I want to talk about is the ASP.Net Validation Summery control, its function is to display all the error messages of the various validation controls in one place instead of displaying the messages next to every control like we did before. Add some TextBoxes with different validation controls then add the ASP.Net Validation Summery control under the btnSubmit and layout your controls like that:

ASP.Net Validation Controls

The ASP.Net Validation Summery control shows what's in the ErrorMessage property of the validation controls, this is useful when the design of your page does not allow you to display the error message next to the controls. If you use the Validation Summery control, you can use the Text property of the validation control to display something like "*" next to the control and have the Validation Summery show the complete messages.

ASP.Net Validation Controls
Rating: 2.2/5 (26 votes)
Bookmark and Share
[del.icio.us] [Digg] [Facebook] [Furl] [Google] [MySpace] [Spurl] [Squidoo] [StumbleUpon] [Technorati] [Yahoo!] [Email]

Post Your Comment: (English Only Please)


(Required)


(Optional, will not be shown)


(Including http:// - Optional)



Type the sum of 5 + 3 (Required)

Copyright © 2007-2009 Hazem Osman. All rights reserved. Terms & Conditions