Thursday, June 13, 2013

Solving common ASP.NET user interactions with simple JavaScript

Providing a string to the user so he can use it in his clipboard.

Forget the keyword "clipboard" or you will go nuts with all the cross-browser support for clipboard methods.
A simple workaround with JavaScript is showing a dialog with the string pasted and already selected. All the user has to do is making a CTRL+C himself.

    function copyToClipboard (text) {
        window.prompt ("Copy to clipboard: Ctrl+C, Enter", text);


Creating a dialog so that the user the confirm if he's sure he wants to proceed with the selected action. If he hits yes ("ok") the action will run (the button onclick event will be called).
If he hits no ("cancel") nothing will happen.

Use the OnClientClick attribute to call a confirmation dialog and prevent the Server Side Event OnClick to be called.

<asp:Button Id="btn" OnClientClick="return confirm('Are you sure you want delete this account?');" 
      Text="Delete account" runat="server" onclick="btn_Click" />


No comments:

Post a Comment