简体   繁体   English

客户端点击事件,而不是背后的代码

[英]client side click event instead of code behind

How can I make this run at server? 如何使它在服务器上运行?

javascript:
function confirm_delete()
{
  if (confirm("Are you sure you want to delete?")==true)
    return true;
  else
    return false;
}

asp ASP

div.Attributes.Add("onclick", "return confirm_delete();");

To capture an event at the server side, you need to use runat="server" : 要在服务器端捕获事件,您需要使用runat="server"

<form runat="server">
    <asp:Button id="button1" Text="Click me!" runat="server" OnClick="confirm_delete" />
</form>

The event handler itself needs to be in the code behind. 事件处理程序本身需要位于后面的代码中。 I don't remember if JScript is supported in ASP.net but confirm definitely isn't. 我不记得是否JScript是在ASP.net支持,但confirm绝对不是。

To have this run on the server, you would want to refactor your application. 要在服务器上运行此程序,您需要重构应用程序。 You would need the confirm_delete function to render the page with a form that confirms their action. 您将需要confirm_delete函数使用确认其操作的表单来呈现页面。 This isn't so bad because you can have it there already just hidden. 这还不错,因为您可以将其隐藏起来。 confirm_delete would hide the normal content and show the confirmation form. confirm_delete将隐藏常规内容并显示确认表单。 The confirmation form would need to have "OK" or cancel buttons, also hooked to back end event handlers that either execute the deletion (I assume this is where the server OnClick is already wired) or take you back to the full page view. 确认表单将需要具有“确定”或“取消”按钮,还必须与后端事件处理程序挂钩,该后端事件处理程序可以执行删除操作(我假设这是服务器OnClick已连接的位置),或将您带回到完整页面视图。

It's much more complex than a JavaScript confirm popup but its not too awful to do. 它比JavaScript确认弹出窗口要复杂得多,但是这样做并不可怕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM