Introduction:

In this article we will explain how to prevent your web page images from being copied by another one or how to disable right click on webpage in asp.net with example.

Description:

Sometimes it is need to prevent your web page images from being copied by another one. You can secure your images by disabling right click or by disabling image context menu on images only. This is an effective approach to prevent your images from being copied or stolen. Here I am going to share the simple example how can you achieve this. This example 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

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
     Inherits="WebApplication1.WebForm1" %>
 
<!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>Right Click</title>
   <script type="text/javascript">
       function disableRightClick() {
            alert("Sorry, right click is not allowed !!");
            return false;
       }
   </script>
</head>
<body>
   <form id="form1" runat="server">
       <asp:Image ID="img1" runat="server" ImageUrl="1.PNG"
         oncontextmenu="return disableRightClick();"/>
       <img alt="MyImage" src="1.PNG" oncontextmenu="returndisableRightClick()"/>
   </form>
</body>
</html>

Disable right click on web page

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

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
     Inherits="WebApplication1.WebForm1" %>
 
<!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>Right Click</title>
   <script type="text/javascript">
       function disableRightClick() {
            alert("Sorry, right click is not allowed !!");
            return false;
       }
   </script>
</head>
<body oncontextmenu="return disableRightClick()">
   <form runat="server">
   SQL Tutorials Point
   </form>
</body>
</html>

Show alert on right click

<script type="text/javascript">
       function disableRightClick() {
            alert("Sorry, right click is not allowed !!");
            return false;
       }
</script>

Disable right click on web page

In above example I explain how can you disable right click on webpage and images. I hope after reading this article you will be able to do this. 

comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like