Verify control usage in Asp.net

Source: Internet
Author: User

In Asp.net, there are a series of verification controls that can easily verify whether the data entered by the user is legal. Although these verification controls are powerful and easy to use, they have one drawback: when using them, when submitting a webpage, user input on the entire web page must be verified. In the verification control, there is no direct method to verify the input of some parts of the page. This article describes how to use ASP. NET to verify controls and JavaScript features to complete the function of verifying only some parts of the page.

Why do I only need to verify the input of some parts of the page? For example, in some applications, sometimes for the convenience of user input, it is possible that in the same page, there are multiple forms that require users to enter and submit data (this is common in some background management systems of information systems ). For example, if you enter the quantity of each incoming goods and submit an operation, you only need to verify the submitted data, while other forms on the page, for example, because the user does not fill in the data for each shipment, it does not need to be verified, which also improves the efficiency of the application.

Next, let's take a look at how to selectively verify the form input. First, we will have a comprehensive understanding of some attributes and usage of the input verification control in Asp.net.

First, we will introduce the causesvalidation attribute. If you do not want to verify the event submitted by a button, you only need to set the causesvalidation attribute to false. For example, if you have a "cancel" button, you can set it as follows:

<Asp: button id = "canonical cancel" runat = "server" text = "cancel" causesvalidation = "false"> </ASP: button>

Another method is:

Optional cancel. causesvalidation = false;

In this way, once the causesvalidation attribute is set to false, both client verification and server verification will not work.

If you want to selectively verify some parts of the page, you need to use some methods of the verification control and javascrpt. The following list lists some methods and attributes of the verification control:

Name Description
Page_isvalid checks whether all input on the page is valid and returns a Boolean value.
An array formed by all verification controls on the current page of page_validators
Page_validationactive is a Boolean value that indicates whether verification is performed. If it is set to false, the verification function is disabled.
Isvalid this property verifies whether the client input is valid
Validatorenable (Val, enable) This method uses a verification control as a passing parameter to start or disable the control from using the verification function, and takes effect on the client.

Here, we will introduce the validatorenable method, which can disable a control from using the verification function, for example:

<Script language = "JavaScript">
Validatorenable (verify the control name, false)
</SCRIPT>

If you want to disable all verification controls on the page to use the verification function, you can use the page_validators array (the elements in this array are all verification controls on the page), using the followingCode

<Script language = "JavaScript">
For (I = 0; I <page_validators.length; I ++)
{
Validatorenable (page_validators [I], false)
}
</SCRIPT>

In actual use, we can combine the above Code to selectively verify the form input. Let's look at a specific example. What we need to achieve is, after you enter only the value of region name and click "Get Report", the server only verifies the input of region name, if the user does not use the input start and end dates, no verification is performed and error information is not displayed. If the user enters the start and end dates and submits them, the system does not verify the input of region name.

The Code is as follows.

<Script language = "JavaScript">
Function enableregionvalidators ()
{
For (I = 0; I <page_validators.length; I ++)
{
Validatorenable (page_validators [I], false)
}
Validatorenable (rvregion, true)
}
</SCRIPT>

Assume that the name of the submit button is set to "cmdregion", while rvregion is a verification control to check whether the user has entered the region name. In order to use the client script () add the following code:

Cmdregion. Attributes. Add ("onclick", "enableregionvalidators ();");

In the above Code, when you click the submit button (cmdregion), besides the verification control named rvregion, other verification controls do not work (for example, you can not enter a date ). However, the above Code only checks the verification control of the client. To disable some verification controls on the server, you must add the following code in the commit event of pai_region:

Private void cmdregion_click (Object sender, system. eventargs E)
{
Rvstartdate. isvalid = true;
Rvenddate. isvalid = true;
Lblreport. Text = "region WISE report for" + txtregionname. Text. toupper ();
}

Set the isvalid attribute of the rvstartdate and rvenddate verification controls to true. These two verification controls are forced to pass verification. Therefore, even if you do not enter the start and end dates, no error information is displayed.

Okay. Let's test our code. First enter a value in Region name, such as China, and submit
We can see that at this time, we did not enter the start date and end date, but the system did not prompt an error message. Similarly, we only enter the start date and end date without entering the region name, no error message is displayed.

ProgramThe main code is as follows:

Disableclientvalidation. aspx:

<Form ID = "form1" method = "Post" runat = "server">
<Table id = "Table1" cellspacing = "0" cellpadding = "0" width = "504"
Align = "center" bordercolorlight = "#996633" border = "1">
<Tr>
<TD style = "font-weight: bold" align = "center" colspan = "3"> region wise reports </TD>
</Tr>
<Tr>
<TD> region name </TD>
<TD>
<Asp: textbox id = "txtregionname" runat = "server"> </ASP: textbox> </TD>
<TD>
<Asp: requiredfieldvalidator id = "rvregion" runat = "server" controltovalidate = "txtregionname" errormessage = "region is required"> </ASP: requiredfieldvalidator> </TD>
</Tr>
<Tr>
<TD> </TD>
<TD> </TD>
<TD>
<Asp: button id = "cmdregion" runat = "server" text = "Get Report"> </ASP: button> </TD>
</Tr>
</Table>
<Br>
<Br>
<Table id = "Table2" cellspacing = "0" cellpadding = "0" width = "504" border = "1"
Align = "center" bordercolorlight = "#996633">
<Tr>
<TD style = "font-weight: bold" align = "center" colspan = "3"> date wise reports </TD>
</Tr>
<Tr>
<TD> Start date </TD>
<TD>
<Asp: textbox id = "txtstartdate" runat = "server"> </ASP: textbox> </TD>
<TD>
<Asp: requiredfieldvalidator id = "rvstartdate" runat = "server" controltovalidate = "txtstartdate" errormessage = "start date is required"> </ASP: requiredfieldvalidator> </TD>
</Tr>
<Tr>
<TD> end date </TD>
<TD>
<Asp: textbox id = "txtenddate" runat = "server"> </ASP: textbox> </TD>
<TD>
<Asp: requiredfieldvalidator id = "rvenddate" runat = "server" controltovalidate = "txtenddate" errormessage = "end date is required"> </ASP: requiredfieldvalidator> </TD>
</Tr>
<Tr>
<TD style = "width: 139px"> </TD>
<TD> </TD>
<TD>
<Asp: button id = "Updated Date" runat = "server" text = "Get Report"> </ASP: button> </TD>
</Tr>
</Table>
</Form>
<Script language = "JavaScript">
Function disablescript ()
{
For (I = 0; I <page_validators.length; I ++)
{
// Page_validators [I]. isvalid = true;
Validatorenable (page_validators [I], false)
}
}

Function enableregionvalidators ()
{
For (I = 0; I <page_validators.length; I ++)
{
Validatorenable (page_validators [I], false)
}
Validatorenable (rvregion, true)
}

function enabledatevalidators ()
{< br> for (I = 0; I {< br> validatorenable (page_validators [I], false)
}< br> validatorenable (rvstartdate, true)
validatorenable (rvenddate, true)
}< br>
runat =" server "width =" 344px "Height =" 24px ">
disableclientvalidation. aspx :. main CS code:
private void page_load (Object sender, system. eventargs e)
{< br> cmdregion. attributes. add ("onclick", "enableregionvalidators ();");
expiry date. attributes. add ("onclick", "enabledatevalidators ();");
}< br> private void cmdregion_click (Object sender, system. eventargs e)
{< br> rvstartdate. isvalid = true;
rvenddate. isvalid = true;
lblreport. TEXT = "region WISE report for" + txtregionname. text. toupper ();
}

private void upload date_click (Object sender, system. eventargs e)
{< br> rvregion. isvalid = true;
lblreport. TEXT = "date wise report betwee" + txtstartdate. text. toupper () + "and" + txtenddate. text. toupper ();
}< BR >}

Related Article

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.