Thursday, July 22, 2010

How to disable copy paste and cut on textbox in asp.net?


1. Simple prevent copy paste or cut on a textbox
------------------------------------------------
<asp:TextBox ID="txtPrevent" runat="server"
        oncopy="return false"
        onpaste="return false"
        oncut="return false">
</asp:TextBox>
------------------------------------------
2. Prevent right click on the textbox
----------------------------------------
javascript:
function preventRightClick(event)
{
if (event.button==2)
{
alert("Right Clicking disabled!");
}
}
<asp:TextBox ID="txtRightClickOff" runat="server"
onMouseDown="preventRightClick(event)">
</asp:TextBox>
--------------------------
3. Prevent ctrl key
---------------------------
javascript:
function PreventCtrlKey(e)
{
var code = (document.all) ? event.keyCode:e.which;
if (parseInt(code)==17)
{
alert('Ctrl key disabled');
window.event.returnValue = false;
}
}
<asp:TextBox ID="txtCtrl" runat="server"
onKeyDown="return PreventCtrlKey(event)">
</asp:TextBox>