In ASP. NET, a verification control is provided to verify the input data of the form for convenient form verification. These verification controls are indeed powerful and provide great convenience for form writing programs. However, you may encounter problems when you are not familiar with them. The most common error is that page. isvalid cannot be called before verification.
I think many of my friends have received the book "in-depth access to ASP. NET. This book has been widely circulated among netizens and is indeed a good entry book, but there are also some errors, such as here. This is a program that demonstrates the use of requiredfieldvalidator when discussing verification components:
Requiredfieldvalidator. aspx:
<% @ Page Language = "C #" %>
<Script language = "C #" runat = "server">
Public void page_load (Object SRC, eventargs E)
{
If (page. ispostback)
{
If (page. isvalid)
{
Show. Text = "verified ";
}
Else
{
Show. Text = "not verified ";
}
}
}
</SCRIPT>
<HTML>
<Head>
<Title> </title>
</Head>
<Body>
<Form runat = "server">
<Table>
<Tr> <TD colspan = 2 align = "center"> <B> requiredfieldvalidator control demonstration </B> </TD> </tr>
<Tr>
<TD> name: </TD>
<TD> <asp: textbox id = "name" runat = "server"/> *
<Asp: requiredfieldvalidator id = "rfv1" runat = "server"
Controltovalidate = "name"
Display = "static"
> (Enter the name)
</ASP: requiredfieldvalidator>
</TD>
</Tr>
<Tr>
<TD> Age: </TD>
<TD> <asp: textbox id = "Age" runat = "server"/> *
<Asp: requiredfieldvalidator id = "rfv2" runat = "server"
Controltovalidate = "Age"
Display = "static"
> (Enter the age)
</ASP: requiredfieldvalidator>
</TD>
</Tr>
<Tr>
<TD> <asp: button text = "Submit" runat = "server"/> </TD>
<TD> </TD>
</Tr>
</Table>
<HR>
<Asp: Label id = "show" forecolor = "red" runat = "server"/>
</Form>
</Body>
</Html>
An error occurs when you run this program:Page. isvalid cannot be called before verification occurs. Use causesvalidation = true in the event handler of the control or query it after page. Validate is called.
This problem comes from the author's negligence. In fact, when you click the submit button, the program performs verification first. After the verification is passed, it interacts with the server (reload here, page_load is called ).
The root cause of the problem is that when you click an ASP: button control, the form is verified by default. Some may wish to have two buttons in the form. Some of the buttons do not have to be verified, but they must be verified by default, causing a lot of trouble in programming.
In fact, this problem is well solved. The button control has a causevalidation attribute. The default value is true. You only need to set this attribute to false to avoid form verification. When verification is required, you can manually call the verification code: the verification control. Validate () or page. Validate () for verification.