简体   繁体   English

如何触发按钮的客户端和服务器端单击事件?

[英]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.

相关问题 如果按钮使用javascript或jQuery同时具有客户端和服务器端单击事件,如何仅触发服务器端单击事件? - How to trigger only server side click event if button have both client and server side click event using javascript or jQuery? ASP按钮单击不会在服务器端触发 - ASP Button Click not fire in server side 如何在服务器端侦听客户端事件 - How to listen to Client Side events on Server Side 如何在单击按钮时更改HTML元素的属性值(在客户端和服务器端)? - How to change the value of an HTML element's attribute (on both client and server side) on the click of a button? 单击按钮即可同时执行客户端和服务器端功能-特定方案 - Performing both client side and server side functions on button click - Specific Scenario 点击表格行以同时触发javascript和.net服务器端代码 - Click on table row to fire both javascript and .net server side code 客户端验证根据按钮单击时的空文本框触发 - Client-side validation fire according to empty textboxes on button click 提交按钮的OnClick事件如何调用服务器端代码和客户端代码? - How can Submit Button's OnClick Event invoke both Server-Side and Client-Side Code? 服务器端按钮仅在客户端返回确认时单击 - server side button click only when client side returns confirm SignalR 客户端方法未从服务器端按钮单击触发 - SignalR client side method not firing on from server side button click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM