Mostly used and essential jQuery code examples

http://jquerybyexample.blogspot.com/2010/12/disable-cut-copy-and-paste-function-for.html

How to Disable right click using jQuery

You can find many java script code snippets to disable right click. But jQuery makes our life easy. Below jQuery code disables the right click of mouse.
1$(document).ready(function(){
2    $(document).bind("contextmenu",function(e){
3        return false;
4    });
5});        


We just need to bind the contextmenu event with the document element.

Method 2 (Send from one of the reader):

1$(document).ready(function(){
2    $(document).bind("contextmenu",function(e){
3        e.preventDefault();
4    });
5});                                                                         

Enable or Disable controls using jQuery in asp.net

You must have come across a situation where you have to make any element disable or enable.There are couple of ways using them you can enable/disable any element using jquery. jQuery really makes your task very easy. In below given example, I have disabled a textbox with id "txtName".

Approach 1
1$("#txtName").attr("disabled", true);
Approach 2
1$("#txtName").attr("disabled", "disabled");
If you wish to enable any element then you just have to do opposite of what you did to make it disable. However jQuery provides another way to remove any attribute.

Approach 1
1$("#txtName").attr("disabled", false);
Approach 2
1$("#txtName").attr("disabled", "");
Approach 3
1$("#txtName").removeAttr("disabled");
That's is. This is how you enable or disable any element using jQuery.

How to Use jQuery in ASP.NET

Using jQuery with ASP.NET

As this post is about "using jQuery with ASP.NET" so we will not be looking into "What is jQuery" and "How to use jQuery" assuming you know basics of jQuery. If not, then please read "Learn how to use jQuery?"

To begin with using jQuery with ASP.NET, first download the latest version the jQuery library from jQuery website and unzip or copy the file in your project or Visual studio solution. Microsoft Visual studio 2010 and 2012 include jQuery by default and provide intellisense to use jQuery. Assuming that you have placed the library in Script folder, add this in the head of your ASP.NET page (simple or master). (Its a good practice to keep your all js file under Script folder).
1<script src="Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
Or you can directly refer them using various CDNs. Put this line of code in head section of ASP.NET Page.
1<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
In the above code, I have not used "http" protocol while referencing jQuery from Google CDN. Its not a mistake rather always use protocol less URL for referencing jQuery.

After this setup, you can use jQuery in your ASP.NET page. Let's see a demo.

Show alert window on click of ASP.NET Button.

Assuming a ASP.NET button with ID "btnSubmit " is placed on the page.
1<asp:Button ID="btnSubmit" runat="server" Text="Button" />
And now bind the click event to ASP.NET Button in document.ready section.
1<script type="text/javascript">
2  $(document).ready(function() {
3    $("#btnSubmit").click(function() {
4         alert("Alert using jQuery");
5    });
6  });
7</script>
In above jQuery code block, we have attached click event to the button using ID selectors. Read more about other jQuery selectors and how to use them.

Below is a list of useful jQuery code example for ASP.NET controls that we use on daily basis. One thing, while creating object of any ASP.NET control, always use ClientID. As when Master pages are used then the ID of the ASP.NET controls is changed at run time. Read more here. But with ASP.NET 4.0, this is changed and now you have control over the Client ID using ClientIDMode property.

Get label value:
1$('#<%=Label1.ClientID%>').text();
Set label value:
1$('#<%=Label1.ClientID%>').text("New Value");
Get Textbox value:
1$('#<%=TextBox1.ClientID%>').val();
Set Textbox value:
1$('#<%=TextBox1.ClientID%>').val("New Value");
Get Dropdown value:
1$('#<%=DropDownList1.ClientID%>').val();
Set Dropdown value:
1$('#<%=DropDownList1.ClientID%>').val("New Value");
Get text of selected item in dropdown:
1$('#<%=DropDownList1.ClientID%> option:selected').text();
Get Checkbox Status:
1$('#<%=CheckBox1.ClientID%>').attr('checked');
Check the Checkbox:
1$('#<%=CheckBox1.ClientID%>').attr('checked',true);
Uncheck the Checkbox:
1$('#<%=CheckBox1.ClientID%>').attr('checked',false);
Get Radiobutton Status:
1$('#<%=RadioButton1.ClientID%>').attr('checked');
Check the RadioButton:
1$('#<%=RadioButton1.ClientID%>').attr('checked',true);
Uncheck the RadioButton:
1$('#<%=RadioButton1.ClientID%>').attr('checked',false);
Disable any control:
1$('#<%=TextBox1.ClientID%>').attr('disabled', true);
Enable any control:
1$('#<%=TextBox1.ClientID%>').attr('disabled', false);
Make textbox read only:
1$('#<%=TextBox1.ClientID%>').attr('readonly', 'readonly');