Difference between onclick () and onClientClick ()?

If I use both onclick() and onClientClick() , can I make sure that the server side will be called only after the function on the client side returns TRUE or vice versa?

For example:

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Import Namespace="System.Xml" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <% protected void save_n_display(object sender, EventArgs e) { // This must be called when validate() returns true... } %> <asp:Button ID="Button1" OnClientClick="validate()" onClick="save_n _display" "Text="Check" runat="server" /> <script type="text/javascript" language="javascript"> function validate() // client side Validation is done { } </script> 

Can I use onclick() and onClientClick() or do I need something else for this? I even tried passing variables from javascript to asp functions, so when validate returns true then save_n _display will be called.

+6
javascript
source share
2 answers

However, you get a click event registered on the client, it does not matter. Although, if you are using a server control, you want to use onclientclick. But the key is that you want to use return Validate (). Then, in your validate method, you return a true or false value depending on whether it was validated or not.

EDIT: make onclientclick look like this:

 onclientclick="return Validate();" 

Then in the check function:

 function Validate() { return true; } 
+4
source share

I need to find an example ... but I just made an on-click event. In codebehind, I ran my ServerSide code ... or I would have registered a script on the page to trigger a JS check and then run the code for the code.

Learn About RegisterStartupScript

 OnClick="MyCodeBehindMethod()" 

Then in the code is:

  //Verify and validate conditions string script1="<script language=JavaScript>" script1 += "Validate();" script1 += "</script>" Page.RegisterStartupScript("clientscript",script1); 
0
source share

All Articles