Disable right click context menu, cut, copy, paste operations on ASPNET Textbox using jQuery

Context menu on textboxes are useful and comes handy a lots of time but sometimes you may want to disable it for certain reasons like disable copy paste on username, password box or disable textboxes that are expecting same value in two textboxes to avoid any typo like confirm email address.




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ContextMenu.aspx.cs" Inherits="ContextMenu" %>


<!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 runat="server">


<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.2.min.js"></script>

<script type="text/javascript">

$(document).ready(function() {

// getting all textboxes on the page

var _txtboxes = $('input:text');

// iterating through all textboxes to attach events on them

$.each(_txtboxes, function(val, index) {

$('#' + index.id).mousedown(function(e) {

// check if mouse click is right click (2) (for left click: 0; middle button click:1)

if (e.button == 2) {

// preventing default behaviour, which is a context menu for Textbox


alert('Right click is disabled');



// binding cut,copy,paste events on textboxes to diable cut,copy,paste operations

$('#' + index.id).live('cut copy paste', function(e) {

// preventing default behaviour for Textbox


alert('Cut, Copy, Paste operations are disabled');








<form id="form1" runat="server">


<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>

<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>

<asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>






Tags: , , ,

ASP.NET | jQuery