Disable Copy and Paste TextBox Text In ASP.NET

Sometimes there is a need to disable the copy & paste feature from the ASP.Net text box. This article provides the details of how to disable the copy & paste feature in a TextBox. However you can use it for div, paragraph tag, form tag and so on which are also applicable.
Let us create an application to do this, as:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New web Site" - "C#" - "Empty WebSite" (to avoid adding a master page).
  3. Provide the web site a name such as disableCopyPaste or another as you wish and specify the location.
  4. Then right-click on Solution Explorer - "Add New Item" - "Default.aspx" page.
  5. Drag and drop one textbox onto the <form> section of the Default aspx page.
Disable Copy and Paste in an ASP.NET Webpage TextBox without writing JavaScript; we need to set the following properties of the TextBox:
  • oncopy="return false"
  • onpaste="return false"
  •  oncut="return false"
 Now the source code of the Default.aspx should look as in the following:
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"  Inherits="_Default" %>

 <!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 id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <%--added by vithal wadje--%>
    <div>
        <asp:TextBox ID="TextBox1" runat="server" oncopy="return false" onpaste="return false"
            oncut="return false" TextMode="MultiLine">
        </asp:TextBox>
    </div>
    </form>
</body>

</html>

Now run the application that looks as in the following:
 
Now copy the above data and past it outside in Notepad or any other tool it can not be copied with. Similarly, copy the Notepad or any other data into the above Text box; it cannot be copied.
Note:
  • The above events oncopy ,onpaste,oncut internally uses the javascript which we don't need to write same as Asp.net validation controls.
Summary
I hope this article is useful for all readers if you have any suggestion then please contact me.

2 comments

Can we achieve the same in html textbox control also?

Reply

Not tested for html textbox but it will not be for HTML textbox

Reply

Post a Comment

www.CodeNirvana.in

Protected by Copyscape
Copyright © Compilemode