javascript 如何同时触发按钮的客户端和服务器端点击事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14457498/
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
How to fire both client side and server side click events of button?
提问by RGR
For a requirement, I need to fire both client side and server side click events of a button in ASP.NET. Is that possible? I have tried like the below. But it does not fire client side click event.
对于一个要求,我需要在 ASP.NET 中触发一个按钮的客户端和服务器端点击事件。那可能吗?我试过如下。但它不会触发客户端点击事件。
[ASPX]
[ASPX]
<asp:Button ID="Button1" runat="server" Text="Disable" OnClick="disable" CausesValidation="false" OnClientClick="return click();" />
[Script]
[脚本]
function click()
{
alert("hi")
}
回答by Joe Ratzer
Yes, it's possible. If the client side Javascript returns true
then the server-side method will get called.
是的,这是可能的。如果客户端 Javascript 返回,true
则将调用服务器端方法。
For example:
例如:
function click()
{
return confirm("Do you want to continue?");
}
回答by Erhan Akp?nar
<asp:Button ID="Button1" runat="server" Text="Disable" OnClick="disable" CausesValidation="false" OnClientClick="click();" />
function click()
{
alert("hi");
this.click();
}
回答by Rahul R.
The client side function must be called in your code. Only thing is depending on what it returns the server side function will get called:
必须在您的代码中调用客户端函数。唯一的事情取决于它返回的服务器端函数将被调用:
function Click() {
alert('Hi');
if (somecondition) {
return true;
}
return false;
}
Also check your server side event handler if it is named correct as you mention in your code "disable". I think this isnt proper name.
还要检查您的服务器端事件处理程序是否如您在代码“禁用”中提到的那样命名正确。我认为这不是正确的名称。
Also check if you really need CausesValidation attribute? as this would have been for your validater which might no longer needed as client side function is manually called.
还要检查您是否真的需要 CausesValidation 属性?因为这将用于您的验证器,因为手动调用客户端功能,该验证器可能不再需要。