As we know copy/paste can be done using either the right click of the mouse or using keys control+c, control+v etc.
So to disallow users to copy or paste we need to disable right click of the mouse as well as control+c, control+v on the required controls (textbox etc).
We can use javascript to disable right mouse click or ctrl keys to ensure user is not able to copy paste in a textbox
we can achieve this in 2 ways
1. use this method when you don't want any alerts or message
<asp:TextBox ID="TextBox1" runat="server"
oncopy="return false"
onpaste="return false"
oncut="return false">
asp:TextBox>
2. If you want to show alerts than use this method instead
Right this javascript function in the head section of aspx page, in
this function we are disabling right mouse click and ctrl keys
function DisableCopyPaste()
{
alert("This functionality has been disabled !");
window.clipboardData.clearData("Text"); //for cleaning up the clipboard
// Cancel default behavior
event.returnValue = false;
}
First register the event handlers for copy & paste events on this textbox.
This can be done in Page_Load() event handler as:
protected void Page_Load(object sender, EventArgs e){
//for paste
txtFname.Attributes.Add("onpaste", "JavaScript: DisableCopyPaste();");
//for copy
txtFname.Attributes.Add("oncopy", "JavaScript: DisableCopyPaste();");
}