"Get Started with ASP 15" the first ASP. NET application-Input validation

Source: Internet
Author: User

Objective

The so-called input verification, as the name implies is to verify that the user input is not met requirements. We have completed this simple application earlier, but there is one more problem to solve: Users can use the Default.aspx form

Submit any data, or even a form that does not contain any data at all. We need to make sure that the user fills out all the form fields to collect all the relevant data, know who will attend the party, and who refuses to attend the party. The following is how you can implement this verification process.

Main content
    • The concept of client authentication and server-side validation
    • Data Annotation Usage
Operations and analysis

Asp. NET provides a variety of different authentication methods, the most common method is to apply attributes to the data model class to implement validation. The 8th chapter will discuss the verification again, and the third part will also cover this topic in detail. But now, the basic validation is applied to the Guestresponse class only in code listing 1-18.

1. Modify the Guestresponse class

To verify which field of the model, you only need to add the appropriate annotations on the model class properties. This is shown in the following code listing:

Code Listing 1-18: Applying validation attributes to the Guestresponse class

The Required attribute in the System.ComponentModel.DataAnnotations namespace tells asp: The attribute that has the "Required" attribute applied must have a value and cannot be empty. Because this attribute has been applied to all properties in the Guestresponse class, this is tantamount to telling ASP. Properties for all data model classes are required. This is a very basic validation because it does not check whether the relevant value is useful, just to make sure that the user has applied the value, but this is enough for this example.

Tip In addition to required, there are other validation features available. The third part of this book will cover other validation features

2. Modify the Default.aspx page code

When you submit a form by clicking the Submit button in the Default.aspx file, the ASP. NET Framework calls the Page_Load method in the Default.aspx.cs code-behind file. This chapter describes how to invoke the TryUpdateModel method to perform model binding in the previous section. After you add the required attribute, this method checks to make sure that the values of all the properties are received. The last step is to add validation attributes to the Guestresponse class, and now you need to increase the content in the Default.aspx file so that you can display an error message when verifying that the form data published by the user encounters a problem. What you need to add is shown in Listing 1-19.

Code Listing 1-19: Displaying validation errors to the user in the Default.aspx file

We have added an ASP. NET Web Form control. Control will generate HTML in the page.

You can use a variety of different controls, which provide a simple way to encapsulate functionality for reuse throughout your application. You can create your own controls, or you can use the controls provided by Microsoft. The third section describes all the knowledge you need to know about controls.

This example adds the ValidationSummary control, which is provided by Microsoft, to display validation errors. This control generates an HTML code block that lists the validation issues that are encountered when processing form data (all validation results are displayed in this control).

Start the application, and then click the Submit RSVP button without entering any data to see how this control works, as shown in 1-14. You can define a CSS style to highlight related errors, and this will be discussed in detail in the third section of the validation.

Next, focus on the last validation error message--a message that tells the user to fill in the Willattend field. When you define the Willattend property in the Guestresponse class, you define a nullable bool value that can be true and false, but can also be null. This feature is used to determine when a user has selected a value for the Id=willattend Select element:

When the server executes TryUpdateModel for model binding, there is some useful interaction between the model-bound process and the required validation feature that can be exploited. The model binding process converts the empty string value of the first OPTION element to NULL, and if it does not generate a value of true or FALSE, the required attribute generates a validation error. With this mismatch, if the user does not select the Yes or no value from the drop-down list, the program automatically generates an error "no value entered".

The only problem with this approach is that the authentication message doesn't make any sense to the user and the user doesn't know that it's labeled "Would you attend?" The Select element corresponds to the data model property named Willattend, that is, the user may not know that the "willattend field is a must" error hint is what is going on, exactly which input except for the problem (above Name,email, Phone a few error hints more intuitive, easy to determine which input input problems. To solve this problem, you need to provide a different display message for the required feature, as shown in Listing 1-20.

Code Listing 1-20: providing a custom validation message in the Guestresponse class

Setting the ErrorMessage property to a more useful message, launching the application, and then submitting the form again without entering any data will see the prompt message in the browser, 1-15. After completing the above task, the sample application is complete, which satisfies all the requirements set at the beginning. Invitees can submit replies, but only after they have filled out all the form fields. Friends can see a list of people who accept invitations and refuse invitations, and make the appropriate arrangements for them.

Figure 15 Customizing the validation message

"Get Started with ASP 15" the first ASP. NET application-Input validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.