OnClientClick is a client script, which is generally used to use JavaScript, run in the client, that is, IE, and execute immediately after clicking.
OnClick is a server-side event handling function, which uses C# or vb.net to run on the server side, that is, IIS. After clicking the button, execute postback, and then run it.
If you want to call OnClientClick first and then do not call OnClick event, you should return false in OnClientClick event. If you do not return false, after executing OnClientClick, you will continue to call OnClick event!
The implementation method is as follows:
<asp:Button ID="btn_Save" runat="server" Text="Save" CssClass="button_bak" OnClientClick="return whetherEmpty();" OnClick="btn_Save_Click" />//JavaScript implementation is as follows: <script language="javascript" type="text/javascript">function whetherEmpty(){//alert("KPI information cannot be all empty!");emptflag = false ;var kpiName = document.getElementById("<%=txt_KPIName.ClientID%>").value;var jobgoal = document.getElementById("<%=txt_JobGoal.ClientID%>").value;var weight = document.getElementById("<%=txt_Weight.ClientID%>").value;var standard = document.getElementById("<%=txt_Standard.ClientID%>").value;if (kpiName == "" && jobgoal == "" && weight =="" && standard ==""){emptflag = true ;}if (emptflag){alert("KPI information cannot be all empty!");return false;}}</script>If OnClientClick="return whetherEmpty();" does not have return, even if return false in JavaScript, onclick will still execute!
Here is a description of the difference between onclientclick and onclick
In fact, sometimes dual verification is required between client and server. Why do you say so? Biguo said that when we pass JS verification on the client, we can only enter letters and numbers, and we cannot have special characters! Usually there is no problem, but there is always a shame that people try every means to "do destruction", and at this time, double verification is needed! To put it bluntly, it means that both the server and the client are checked!
For example: Button has: OnClick event and OnClientClick attributes, the former is generally a click event on the server! The latter is a click event on the client!
Let's do a test! Add the following code to the Default.aspx page!
<script language="javascript" type="text/javascript">function buttonClick() {alert("I am the client click event");return false; }</script>Page code:
<form id="form1" runat="server"><div><asp:Button ID="Button1" runat="server" Text="Some Buttons are server controls, and the OnClientClick event is used by default. Please disable the browser's Js function experience"OnClick="Button1_Click" OnClientClick="return buttonClick();" /><asp:Label ID="Label1" runat="server" Text=""></asp:Label></div></form>
cs file code:
protected void Button1_Click(object sender, EventArgs e){this.Label1.Text = "I am the server click event";}Everyone run it and see what the effect is? You may find that only the code in JS pops up! That's right, this is exactly the effect we want. Use return false to "block" the server's verification! What are the benefits? You just don't need to refresh the page!