[英]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? 如果我同时使用
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? 那么我可以使用
onclick()
和onClientClick()
还是需要其他功能? I even tried passing variables from javascript to asp functions so when validate returns true then save_n _display will be called. 我什至尝试将变量从javascript传递到asp函数,因此,当validate返回true时,将调用save_n _display。
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.
尽管如果您正在使用服务器控件,那么您确实希望使用onclientclick。 But the key is that you want to use return Validate().
但是关键是您要使用return Validate()。 Then in your validate method you return a true or false value depending on whether it validated or not.
然后,在您的validate方法中,根据是否已验证,返回一个true或false值。
EDIT: So make onclientclick look like this: 编辑:因此使onclientclick看起来像这样:
onclientclick="return Validate();"
Then in the validate function: 然后在validate函数中:
function Validate()
{
return true;
}
I have to go find the example...but I just did the on-click event. 我必须去查找示例...但是我只是做了on-click事件。 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.
在后台代码中,我运行了ServerSide代码...或者我将在页面上注册脚本以调用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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.