Use js to check the interface before calling the button event in Asp.net

Source: Internet
Author: User

The onclick event of our button calls the server.Code. Use the onclientclick event to call the client code. The Code is as follows:

< ASP: button ID = "Btnsave" Runat = "Server" Cssclass = "BTN" Onclick = "Btnsave_click" Text = "Save" Onclientclick = "Return checkcontrol ();"   />

Note:Btnsave_clickSaves events for the server.

Checkcontrol() To save the js method for checking with JS on the front end, the JS Code is as follows.

Function Checkcontrol ()
{
VaR Vapyname = Document. getelementbyid ( " Txbapyname " ). Value;
If (Vapyname. Length <= 0 )
{
Alert ( " 'Requester name' cannot be blank! " );
Document. getelementbyid ( " Txburllist " ). Focus ();
Document. getelementbyid ( " Txburllist " ). Select ();
Return   False ;
}
}

Note:

Why?OnclientclickAdd return? To the event?

At the beginning, I did not add the return statement, and found that the click event in the background is still executed after the js method is executed. The original code is as follows:

< ASP: button ID = "Btnsave" Runat = "Server" Cssclass = "BTN" Onclick = "Btnsave_click" Text = "Save" Onclientclick = "Checkcontrol ();"   />

Summary:

When we use onclientclick to verify the submitted data, we must return true or false, that is, we must add return, otherwise the onclick is invalid. When false is returned, The onclick server event is aborted.

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.