Disable copy paste right click using javascript in asp.net TextBox

This blog defines How to Disable copy paste right click using javascript in asp.net TextBox. We make two function in JavaScript one for disable right click and other for disable ctrl key. When we create right click on the textbox message will be generate to show disable right click.

Disable right click using javascript

function DisableRightClick(event)

 {

     if (event.button == 2)

      {

         alert("Right Clicking not allowed!");

         }

   }

 

Now calling that method on the onMouseDown event of the TextBox.

 

onMouseDown="DisableRightClick(event)">

 

Disable Ctrl key using javascript

function DisableCtrlKey(e)

        {

            var code = (document.all) ? event.keyCode : e.which;

            var message = "Ctrl key functionality is disabled!";

            if (parseInt(code) == 17)

            {

                alert(message);

                window.event.returnValue = false;

           }

        }

 

Now calling that method on the onKeyDown event of the TextBox.

onKeyDown="return DisableCtrlKey(event)"

.ASPX Code

<head runat="server">

    <title></title>

    <script language="javascript">

        function DisableRightClick(event)

        {

            if (event.button == 2)

            {

                alert("Right Clicking not allowed!");

            }

        }

        function DisableCtrlKey(e)

        {

            var code = (document.all) ? event.keyCode : e.which;

            var message = "Ctrl key functionality is disabled!";

            if (parseInt(code) == 17)

            {

                alert(message);

                window.event.returnValue = false;

           }

        }

    </script>

    <style type="text/css">

        .style1

        {

            font-family: Verdana;

        }

        .style2

        {

            font-size: large;

        }

    </style>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        &nbsp;<span class="style1"><strong><span class="style2">Right click and Ctrl key

        </span> </strong><span class="style2">disabled.</span></span><br />

        <br />

        <asp:TextBox ID="TextBox2" onKeyDown="return DisableCtrlKey(event)" runat="server"

            onMouseDown="DisableRightClick(event)">

        </asp:TextBox><br />

        <br />

    </div>

    </form>

</body>


Now run the application and test it with right click and ctrl key.