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.