Wednesday, April 18, 2012

how to disable copy, cut and paste (Ctrl + c/Ctrl + v/ Ctrl+ x) options in asp.net textbox

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

 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 textboxtitle>
head>
<body>
<form id="form1" runat="server">
<div>
<strong>First Method To disable copy, cut and paste options in textboxstrong><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 textboxtitle>
<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 textboxstrong><br />
<asp:TextBox ID="txtUser" runat="server" onKeyDown="return DisableControlKey(event)" onMouseDown="return DisableControlKey(event)">asp:TextBox>
div>
form>
body>
html>

No comments:

Post a Comment