javascript onclick() 和 onClientClick() 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3346171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 00:54:48  来源:igfitidea点击:

Difference between onclick() and onClientClick()?

javascriptasp.net

提问by Sangram Nandkhile

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?

如果我同时使用两者onclick()onClientClick()我能否确保只有在客户端函数返回 TRUE 后才会调用服务器端,反之亦然?

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.

所以,我可以使用onclick()onClientClick()或是否需要这个东西有什么不同?我什至尝试将变量从 javascript 传递到 asp 函数,因此当验证返回 true 时,将调用 save_n _display。

回答by spinon

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.

但是,您注册了客户端单击事件并不重要。但是,如果您使用的是服务器控件,那么您确实希望使用 onclientclick。但关键是你要使用return Validate()。然后在您的验证方法中,您根据它是否经过验证返回真值或假值。

EDIT: So make onclientclick look like this:

编辑:所以让 onclientclick 看起来像这样:

onclientclick="return Validate();"

Then in the validate function:

然后在验证函数中:

function Validate()
{
    return true;
}

回答by The Mirage

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.

我必须去找那个例子……但我只是做了点击事件。在代码隐藏中,我运行了我的服务器端代码……或者我会在页面上注册一个脚本来调用 JS 验证,然后运行代码隐藏代码。

Learn about RegisterStartupScript

了解 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);