[英]How to fire both client side and server side click events of button?
For a requirement, I need to fire both client side and server side click events of a button in ASP.NET. 根据要求,我需要触发ASP.NET中按钮的客户端和服务器端单击事件。 Is that possible?
那可能吗? I have tried like the below.
我已经尝试过如下。 But it does not fire client side click event.
但是它不会触发客户端单击事件。
[ASPX] [ASPX]
<asp:Button ID="Button1" runat="server" Text="Disable" OnClick="disable" CausesValidation="false" OnClientClick="return click();" />
[Script] [脚本]
function click()
{
alert("hi")
}
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?");
}
<asp:Button ID="Button1" runat="server" Text="Disable" OnClick="disable" CausesValidation="false" OnClientClick="click();" />
function click()
{
alert("hi");
this.click();
}
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? 还检查您是否真的需要CausesValidation属性? as this would have been for your validater which might no longer needed as client side function is manually called.
因为这将用于您的验证器,因为手动调用客户端功能可能不再需要该验证器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.