Introduction
Here I will explain how to disable copy, cut and paste functionality (Ctrl + c/Ctrl + v/ Ctrl+ x) in asp.net textbox using JavaScript.
Description
In previous post I explained clearly how to disable right click on asp.net web page. Now I will explain how to disable copy, cut, paste or Ctrl+c, Ctrl+v, Ctrl+x options in textbox on aspx page in asp.net. To achieve this we have two methods first method is directly set copy, cut and paste options return false in textbox to disable and second one we can use JavaScript functionality to disable copy, cut and paste options to implement this one first create new website and design your Default.aspx page like this
First Method
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Sample to Disable Copy, Cut and Paste Options in textbox</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<strong>First Method To disable copy, cut and paste options in textbox</strong><br />
<asp:TextBox ID="TextBox1" runat="server" oncopy="return false" oncut="return false" onpaste="return false"></asp:TextBox>
</div>
</form>
</body>
</html>
|
Second Method
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Sample to Disable Copy, Cut and Paste Options in textbox</title>
<script language="javascript" type="text/javascript">
//Function to disable Cntrl key/right click
function DisableControlKey(e) {
// Message to display
var message = "Cntrl key/ Right Click Option disabled";
// Condition to check mouse right click / Ctrl key press
if (e.which == 17 || e.button == 2) {
alert(message);
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<strong>Second Method to Disable copy, cut and paste options in textbox</strong><br />
<asp:TextBox ID="txtUser" runat="server" onKeyDown="return DisableControlKey(event)" onMouseDown="return DisableControlKey(event)"></asp:TextBox>
</div>
</form>
</body>
</html>
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
6 comments :
thanx for making things easier.
Awesome tutorial..Learning this helps me more..Thanks
nice teaching. Second Method you used like this "if (e.which == 17 || e.button == 2)" this condition is working for mouse right click but not working for Ctrl key press."if (e.keyCode == 17 || e.button == 2)" this condition is working fine for both..
Exlpain second method you used (e.which == 17 || e.button == 2) how to work this?
amazing technique...thank u sir
Just add one more attribute to first solution
oncontextmenu = "return false"
to disable right click of mouse.
Note: Only a member of this blog may post a comment.