Tuesday, 16 October 2012

Disable copy past in Textbox in C# Asp.Net


Lets consider a textbox with id=txtsubject. First register the event handlers for copy & paste events on this textbox.

This can be done in Page_Load() event handler as:

txtSubject.Attributes.Add(“onpaste“, “JavaScript: DisableCopyPaste();”);   //for paste

txtSubject.Attributes.Add(“oncopy“, “JavaScript: DisableCopyPaste();”);   //for copy

Now DisablePaste() funtion in Javascript:

function DisableCopyPaste()

        {

            alert(‘This functionality has been disabled !‘);          

            window.clipboardData.clearData(“Text“); //for cleaning up the clipboard

            // Cancel default behavior

            event.returnValue = false;

        }

No comments:

Post a Comment