Loading

Disable Right Click and Ctrl Keys in ASP.Net

Submitted by: 


We can easily disable right click and control keys in asp.net.In come circumstances.We want to disable the right click so that the user can not copy from or to the asp.net web page.In most of the web application it can be use in any possible condition.
for doing this we have two possible ways:

1). Using asp.net method, which is used when we don't want to show message to the user

  1. <asp:TextBox ID="TextBox1" runat="server"
  2. oncopy="return false"
  3. onpaste="return false"
  4. oncut="return false">
  5. </asp:TextBox>

In this we are making "oncopy","onpaste","oncut" we used "return false"

2). Using JavaScript, which will provide a message to the user(alert message)

for doing this simply add java script into the head section of the web page where we want to restrict the user form the specified keys

  1. <head runat="server">
  2. <title>Untitled Page</title>
  3. <script language="javascript">
  4. function DisableRightClick(event)
  5. {
  6. //For mouse right click
  7. if (event.button==2)
  8. {
  9. alert("Right Clicking not allowed!");
  10. }
  11. }
  12. function DisableCtrlKey(e)
  13. {
  14. var code = (document.all) ? event.keyCode:e.which;
  15. var message = "Ctrl key functionality is disabled!";
  16. // look for CTRL key press
  17. if (parseInt(code)==17)
  18. {
  19. alert(message);
  20. window.event.returnValue = false;
  21. }
  22. }
  23. </script>
  24. </head>

Note: you can place this function to MASTER PAGE, if we want this into many different pages

Now we have to call this function into our pages, using this coding

  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <strong>
  5. Right click disabled</strong> textbox
  6. <br />
  7. <asp:TextBox ID="TextBoxCopy" runat="server"
  8. onMouseDown="DisableRightClick(event)">
  9. </asp:TextBox><br />
  10. <br />
  11. <strong>Ctrl key </strong>disabled<br />
  12. <asp:TextBox ID="TextBox2" runat="server"
  13. onKeyDown="return DisableCtrlKey(event)">
  14. </asp:TextBox><br />
  15. <br />
  16. Another method to disable<strong> Cut,Copy and paste
  17. </strong>in textbox<br />
  18. <br />
  19. <asp:TextBox ID="TextBox1" runat="server"
  20. oncopy="return false"
  21. onpaste="return false"
  22. oncut="return false">
  23. </asp:TextBox>
  24. </form>
  25. </body>

About the author:

PlanetSourceCode.in is a place for all developer providing free source codes, articles, complete projects,complete application in PHP, C/C++, Javascript, Visual Basic, Cobol, Pascal, ASP/VBScript, AJAX, SQL, Perl, Python, Ruby, Mobile Development




Comments

In this code funtion ParseInt(code)==17 is used.
From this what is the value for 17.
What it should be returned..?

1) 17 == CTRL Key

2) True / False -- It is checking the number for every key that is pressed and if CTRL is pressed then the warning pops up...that is if you set it up correctly.

Pages

Add new comment

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.