Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between onclick() and onClientClick()?

If I use both onclick() and onClientClick() can I make sure that server side will be called only after client side function 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>

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

like image 908
Sangram Nandkhile Avatar asked Jul 27 '10 17:07

Sangram Nandkhile


1 Answers

However you get your client side click event registered it doesn't matter. Though if you are using a server control then you do 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 validated or not.

EDIT: So make onclientclick look like this:

onclientclick="return Validate();"

Then in the validate function:

function Validate()
{
    return true;
}
like image 95
spinon Avatar answered Oct 09 '22 04:10

spinon