简体   繁体   中英

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

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

To capture an event at the server side, you need to use 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.

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. 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. 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.

It's much more complex than a JavaScript confirm popup but its not too awful to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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