ASP. NET -- RequiredFieldValidator control and ValidationSummary control,

Source: Internet
Author: User

ASP. NET -- RequiredFieldValidator control and ValidationSummary control,

When we log on to the webpage, if we forget to enter the password and say the user name, there will always be a small reminder on the webpage.

So how can we implement this function when we make it online? This uses the RequiredFieldValidator control and ValidationSummary control. These two controls are combined to achieve the desired effect.

Take the preceding logon example as an example:

<Asp: TextBox ID = "txtName" runat = "server" CssClass = "txtName" OnTextChanged = "txtCode_TextChanged"> </asp: TextBox> <asp: textBox ID = "txtPWD" runat = "server" CssClass = "txtName" OnTextChanged = "txtCode_TextChanged"> </asp: TextBox> <asp: requiredFieldValidator ID = "RequiredFieldValidator1" runat = "server" ControlToValidate = "txtName" ErrorMessage = "enter account" Text = "enter account"> </asp: RequiredFieldValidator> asp <: requiredFieldValidator ID = "RequiredFieldValidator2" runat = "server" ControlToValidate = "txtName" ErrorMessage = "Enter Password" Text = "Enter Password"> </asp: RequiredFieldValidator> asp <: button ID = "btnLogin" runat = "server" CssClass = "btnLogin" Text = "Submit" OnClick = "btnLogin_Click"/> <asp: validationSummary ID = "ValidationSummary1" runat = "server" ShowMessageBox = "true" ShowSummary = "false"/>


The difference is that a prompt box is displayed for the example in the code, and corresponding words are prompted after the text box. To implement this function, set the ShowMessageBox attribute of the ValidationSummary control to True and the ShowSummary attribute to False.


In ASPNET, whether data verification is to drag a ValidationSummary control and then drag several RequiredFieldValidator controls

You also need to set their verification objects. For example, you need to verify the control.

(ASPNET) What are the verification controls? What is the role?

Verify the usage of the control
① RequiredFieldValidator Control
When the control on the page requires that data be input, RequiredFieldValidator takes effect. Select the control to be verified for the ControlToValidate attribute, and the ErrorMessage attribute indicates the error message displayed after the verification is invalid.
<HTML>
<HEAD>
<Title> RequiredFieldValidator Example </title>
</HEAD>
<BODY>
<Form id = "Form1" runat = "server">
Name:
<Asp: TextBox id = "TextBox1" runat = "server"> </asp: TextBox>
<Asp: RequiredFieldValidator id = "RequiredFieldValidator1" runat = "server"
ErrorMessage = "Please enter your name" ControlToValidate = "TextBox1"> </asp: RequiredFieldValidator>
<Asp: Button id = "Button1" runat = "server" Text = "Button"> </asp: Button>
</Form>
</BODY>
</HTML>

② CompareValidator Control
The CompareValidator control is used to compare data consistency between two input controls. It can also be used to verify the data types of the control content, such as integer and string types. The ControlToCompare and ControlToValidate attributes are used to set the two controls for comparison.
<HTML>
<HEAD>
<Title> CompareValidator Example </title>
</HEAD>
<BODY>
<Form id = "Form1" runat = "server">
<P> Password:
<Asp: TextBox id = "txtPwd" runat = "server" TextMode = "Password"> </asp: TextBox> <BR>
Confirm:
<Asp: TextBox id = "TxtCfm" runat = "server" TextMode = "Password"> </asp: TextBox> </P>
<P>
& Lt ...... remaining full text>

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.