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>