Thursday, June 13, 2013

Solving common ASP.NET user interactions with simple JavaScript


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

Solution:
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);
    }

$("input.copyurl").click(function(){
           copyToClipboard($(this).prev().text());
        });


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

Solution:
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" />

Source:
http://stackoverflow.com/questions/1234953/confirmation-dialog-at-runtime-in-asp-net

No comments:

Post a Comment