Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Disable Copy Paste cut options in textbox using javascript in asp.net

Oct 30, 2011
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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

6 comments :

Anonymous said...

thanx for making things easier.

Anonymous said...

Awesome tutorial..Learning this helps me more..Thanks

Anonymous said...

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

Anonymous said...

Exlpain second method you used (e.which == 17 || e.button == 2) how to work this?

Anonymous said...

amazing technique...thank u sir

Anonymous said...

Just add one more attribute to first solution

oncontextmenu = "return false"

to disable right click of mouse.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.