Click here to Skip to main content
15,881,852 members
Articles / Web Development / ASP.NET
Article

Disabling the right click on web page

Rate me:
Please Sign up or sign in to vote.
1.28/5 (39 votes)
12 Aug 20052 min read 258.1K   269   22   15
Article shows two methods to disable context menu on right click on web page first method can be found on majority of web pages, but second method which cannot be found anywhere is simplest

Download demo project - 2.57 Kb

Introduction

Want to protect your source code? Using this code will prevent the vast majority of users from right-clicking over a page and choosing "View source", or right-clicking over an image and saving it.<o:p>

Some times there might be the requirement to disable the pop up menu on click of right button.<o:p>

<o:p> 

Programmers may need to disable right click to prevent user from saving the images on the page or viewing the source of the page.

<o:p> 

Though disabling right click is not complete solution to save the data, but it will make task difficult, or may be impossible for the rookies.<o:p>

 <o:p>

I searched on a net for the solution to this problem and i got following solution.<o:p>

Method 1

      In this method we add a javascript method, in this we check if click is right click or left click if it is right click then a message is displayed like "Right click disabled"<o:p>

<o:p>

<o:p> 

C#
<script language="javascript">
document.onmousedown=disableclick;
status="Right Click Disabled";
Function disableclick(e)
{
  if(event.button==2)
   {
     alert(status);
     return false;	
   }
}
</script>

But in this solution if you click the right click it displays the message <o:p>

“Right click disabled”.<o:p>

<o:p>

<o:p> 

If we want to remove the messegebox then this solution do not work.<o:p>

 

  I am having another simple solution to achieve the same result and also it does not show the messegebox.<o:p>

<o:p>

Method 2

          In this method we set the oncontextmenu="return false" in the body part of the page

so body part will look like

C#
<body oncontextmenu="return false">
...
</body>

So whenever user clicks the right mouse button nothing will happen, no message no context menu.<o:p>

<o:p> 

 Sometimes there might be requirement to disable the right click on specific control to achive this we have to follow these steps.<o:p>

1.      remove added code in body (i.e. oncontextmenu=”return false”)<o:p>

2.      Add one table and in its row add the control e.g. datagrid control<o:p>

3.      put the oncontextmenu=”return false” in the <tr> for that control<o:p>

 <o:p>

So code will look like this<o:p>

C#
<Body>
  <Table>
   <tr oncontextmenu="return false">
    <td>
     <asp:datagrid id="dgGrid1">---</asp:datagrid>
   </td>
  </tr>
 </Table>
</Body>

So by this method context menu on the right click of right mouse click can be disabled. <o:p>

<o:p> 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Rohit Dighe
B.E. Computer from University of Pune, India.

Working on Asp.Net and C# from 2 years

Comments and Discussions

 
GeneralMy vote of 1 Pin
Margo Morel22-Jun-14 10:28
Margo Morel22-Jun-14 10:28 
Questionnice sir Pin
ntitish14-Mar-13 3:46
ntitish14-Mar-13 3:46 
GeneralMy vote of 5 Pin
hadi552625-Jul-12 19:49
hadi552625-Jul-12 19:49 
GeneralMy vote of 5 Pin
shaminders7-Apr-12 23:07
shaminders7-Apr-12 23:07 
GeneralThis helped me. Thanks Pin
NeCroFire22-Nov-10 2:56
NeCroFire22-Nov-10 2:56 
GeneralRe: This helped me. Thanks Pin
hadi552625-Jul-12 19:50
hadi552625-Jul-12 19:50 
Generaluseless trick Pin
anojkumar1-Jul-08 18:47
anojkumar1-Jul-08 18:47 
QuestionHow to disable Copy/Past into a TextBox Pin
Sudipta(Bubun)28-Jun-07 20:22
Sudipta(Bubun)28-Jun-07 20:22 
AnswerRe: How to disable Copy/Past into a TextBox Pin
anil 1kumar23-Oct-07 19:05
anil 1kumar23-Oct-07 19:05 
GeneralOther ways to access the menu Pin
Philippe Mori13-Aug-05 8:14
Philippe Mori13-Aug-05 8:14 
GeneralWaste of time Pin
Anonymous13-Aug-05 7:14
Anonymous13-Aug-05 7:14 
GeneralWhy you shouldn't do this: Pin
NeverHeardOfMe12-Aug-05 12:18
NeverHeardOfMe12-Aug-05 12:18 
QuestionAre you serious?? Pin
JoeySmith7-Aug-05 4:18
JoeySmith7-Aug-05 4:18 
GeneralDon't do this Pin
ector5-Aug-05 10:40
ector5-Aug-05 10:40 
GeneralHTML trick... Pin
PixiGreg5-Aug-05 2:59
PixiGreg5-Aug-05 2:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.