Introduction: Disable right click on web page and images

In this post, we will learn about how to Disable right click on web page and images in asp.net.

Description:

In my previous posts I have explained various topics likeĀ Nullables in C# Collections in C#SortedSet in C#.Net,Stack in C#,Queue in C#.Net,LinkedList in C#.Net. Now in this post, I will explain about how to disable right click on web page and images.

Many a times it is required to prevent your web page images from being copied by someone else.This is an effective way to prevent your images from being copied or stolen.This trick will disable right click only on images. Add the below event handler to the img or image tag as shown below:

Disable right click on images
<asp:Image ID="img1" runat="server" ImageUrl=""../ImgLoc/1.png"" oncontextmenu="return false;" />
<img alt="MyImage" src="../ImgLoc/2.png" oncontextmenu="return false;"/>
Disable right click on web page

You can disable right click on whole page by adding oncontextmenu handler in body tag of webpage. Now this will disable right click on each and every control of a webpage.

<html>
<head>
...
</head>
<body oncontextmenu="return false;" >
...
</body>
</html> 
Show alert on right click
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%3E%20%0Afunction%20disableRightClick()%20%0A%7B%20%0Aalert(%22Right%20click%20is%20not%20allowed%20!!%22)%3B%20%0Areturn%20false%3B%20%0A%7D%20%0A%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" /> 
<body oncontextmenu=" return disableRightClick();">
... 
</body> 

Summary

I hope you got an idea about Disable right click on web page and images.
I would like to have feedback from my blog readers.
I hope after reading this article you will be able to do this.
Your valuable feedback, question, or comments about this article are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website