In this article, we will learn How to Disable Copy/Cut/Paste Options in TextBox. In some situations, we restrict users not to copy or cut or paste web page data. We can achieve this in two methods by directly using return false copy, cut, and paste in TextBox control or by using javascript function. We will see one by one.
1st Method:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frmDisableCopyCutPaste.aspx.cs" Inherits="frmDisableCopyCutPaste" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Disable Copy/Cut/Paste options in TextBox - Method 1</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<strong>Disable Copy/Cut/Paste options in TextBox</strong>
<br />
<asp:TextBox ID="txtDisable" runat="server" oncopy="return false" oncut="return false" onpaste="return false">
</asp:TextBox>
</div>
</form>
</body>
</html>
In the above code, it is very clear that we just attached return false to all oncopy, oncut and onpaste events. By writing this the TextBox won’t allow us to copy or cut or paste options.
2nd Method
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frmDisableCopyCutPaste2.aspx.cs" Inherits="frmDisableCopyCutPaste2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Disable Copy/Cut/Paste options in TextBox using Javascript</title>
<script type="text/javascript" language="javascript">
function disableCopyCutPaste(e)
{
var message="Copy/Cut/Paste options have been disabled...";
if(e.which==17 || e.button == 2)
{
alert(message);
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<strong>Disable Copy/Cut/Paste options in TextBox using Javascript</strong>
<br />
<asp:TextBox ID="txtDisable" runat="server" onKeyDown="return disableCopyCutPaste(event)" onMouseDown="return disableCopyCutPaste(event)"></asp:TextBox>
</div>
</form>
</body>
</html>
In the above code, we used one javascript function to disable copy/cut/paste options in TextBox. When we are trying to copy/cut/paste the content in TextBox, it will alert us saying you are not allow to do Copy/Cut/Paste options in TextBox.
Happy Coding ... !!!!
How to Create Image Slider in Asp.Net using jQuery
How to Disable Copy/Cut/Paste Options in TextBox
How to Implement Scrollable Grid with Fixed Header in Asp.Net using jQuery ?
How to Set Default Home Page in Internet Explorer or Mozilla Browser
How to Read and Write Connection String in Web.Config
How to Disable Browser Back Button using Javascript in ASP.Net
How to Display Excel data in Gridview using Asp.Net
Dynamically Add Check Boxes in Asp.Net Web Page
Related Articles
This post first appeared on Dot Net Programming (C#, Asp.Net, ADO.Net, WCF, WPF, Ajax, LINQ), please read the originial post: here