JavaScript data verification implementation code in asp.net

Source: Internet
Author: User

Today, on a data modification page, there is a DropDownList control bound to an important attribute in the object. I want the user to prompt the user when modifying the control value, he modified the value of the control. Therefore, the following statement is added to the drop-down control.
Copy codeThe Code is as follows:
DDLmaterial. Attributes. Add ("onchange", "alert ('note: You have modified the item type! ')");

It seems good. Not only does it have a prompt effect, but you do not need to refresh the page. However, when I open the page and perform a test, I find the problem. When I change the value of the drop-down control back to the original value, a prompt is displayed, this will clearly give users an inexplicable feeling. What should I do? Use AJAX for server-side verification? This idea is too evil. I decided to try to implement it on the client using JS.
The implementation algorithm is very simple. First, I will search for the JS method that will be executed immediately after the page is loaded to obtain the initial values after the data is bound to the drop-down control. GOOGLE has achieved the following results:
Copy codeThe Code is as follows:
Window. onload = function (){
Var ddl = document. getElementById ('ddlmaterial ');
Var first = ddl. options [ddl. selectedIndex]. text;
}

My understanding is as follows:
Row 1st: After a page is loaded, the function () is executed. The function is a keyword. This is a strange usage.
Row 2nd: Get the control object through the control ID. This var is somewhat like an object.
Row 3rd: obtains the value of the currently selected row of the control.
Now, modify the code to meet my requirements.
// Background code
DDLmaterial. Attributes. Add ("onchange", "changeAlert ()");
Front end
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var first;
Var ddl;
Window. onload = function (){
Ddl = document. getElementById ('ddlmaterial ');
First = ddl. options [ddl. selectedIndex]. text;
}
Function changeAlert (){
Var sencond = ddl. options [ddl. selectedIndex]. text;
If (sencond! = First ){
Alert ("NOTE: You have modified the item type! ");
}
}
</Script>

To sum up, I suddenly found that there was a smart prompt when VS2008 was writing JS Code. (This is really great. The Gospel of cainiao, where does JS Code seem to be executed, I won't tell you if an error occurs.) It seems that I have already timed OUT ....

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.