开发者

Difference between onclick() and onClientClick()?

开发者 https://www.devze.com 2023-01-08 21:37 出处:网络
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?

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="javascri开发者_如何学JAVApt">
    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.


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;
}


I have to go find the example...but I just did the on-click event. In the codebehind, I ran my ServerSide code...or I would register a script on the page to call the JS validation and then run the codebehind code.

Learn about RegisterStartupScript

OnClick="MyCodeBehindMethod()"

Then, in the codebehind have:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号