Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written the following function in javascript and call it to a button.
XML
<script type="text/javascript">
       function CheckValidation() {
           var strMachineCode = document.getElementById('<%= txtMachineCode.ClientID %>').value;
           if (strMachineCode == "") {
               alert("Please provide Machine code");
           }

       }
   </script>

Call to server side button
<asp:Button ID="btnSave" runat="server" CssClass="button" Text="Save" 
            Width="80px" onclick="btnSave_Click" OnClientClick="CheckValidation();" />


But this does not show alert. Thanks in advance.
Posted

1 solution

Why should it?
What you have done is running the OnClientClick, and than went to the server, so your page refresh and the alert message is irrelevant!
If your JS code should validate the page than you should stop the propagation of the server side event by adding return false; after the alert message...
 
Share this answer
 
Comments
Sumon562 19-Oct-14 4:15am    
Thanks for your advice. It works well.
Kornfeld Eliyahu Peter 19-Oct-14 4:15am    
Welcome...
Sumon562 19-Oct-14 4:19am    
I am very regretful to you. Can u please describe what return false does?
Kornfeld Eliyahu Peter 19-Oct-14 4:25am    
It stops the flow of the execution...
The next thing after OnClientClick that should come is OnClick - go to the server...
return false; stops that flow and the post back never happens...
Sumon562 19-Oct-14 4:29am    
Its really interesting. Thank u again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900