Wednesday, 13 June 2012

Disable CUT,COPY,PASTE in your ASP.NET textbox

 Here I will explain how to disable copy, cut and paste functionality (Ctrl + c/Ctrl + v/ Ctrl+ x) in asp.net textbox using JavaScript.

            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.

First Method

<asp:TextBox ID="TextBox1" runat="server" oncopy="return false" oncut="return false" onpaste="return false"></asp:TextBox>


Second Method

JavaScript Function:
         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;
                     }
                  }
            
In .aspx page
<asp:TextBox ID="txtUser" runat="server" onKeyDown="return DisableControlKey(event)"
onMouseDown="return DisableControlKey(event)"></asp:TextBox>

No comments:

Post a Comment