Pages

Saturday, May 23, 2015

How to disable/enable copy paste in ASP.NET without scripting

Hey Guys!

Today, we will see that how to disable / enable the copy paste function in ASP.NET textbox without even using any scripting language.

So first of all, you need to create a test project in MS Visual Studio 2010, then follow the steps below:
  • Start > All Programs > Microsoft Visual Studio 2010
  • File > New Website > ASP.NET Empty Web Site (create your project on your desired location)
  • Right click on the solution explorer, select "Add New Item"
  • Place a ASP.Net control Textbox by drag and drop on the page under the <form> tags.
In order to disable / enable copy, paste and cut functions, use the following properties:

oncopy="return false"
onpaste="return false"
oncut="return false"

So, your markup page should be like this:

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Mytextbox.aspx.vb"  Inherits="Mytextbox"

 <!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">

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


</html>
 Now, run the application on browser and test. Hope so this will be helpful to you.