Using Jquery in ASP
Using Jquery in ASP
NET Page
First of all you will require to link jQuery library to your ASP.NET page. Use below shown code
in HTML code to include latest jQuery.
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js">
</script>
It also can be achieved using server ASP.NET side code like this:
To run any function on page load ASP.NET has inbuilt method to register startup script. Below
shown example will run javascript function "helloWorld" on page load which appends text
"Hello World!!" in the div with id "divSample"
Example:
HTML code :
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>jQuery with asp.net examples - HelloWorld with jQuery</title>
<script type="text/javascript">
function helloWorld()
{
$("#divSample").append("Hello World!!");
}
</script>
</head>
<body>
<form id="form2" runat="server">
<div id="divSample">
</div>
</form>
</body>
</html>
C# Server-side code:
Now you have idea how to include jQuery in ASP.NET page and run script on page load. We
will see how to pass data to server and get response from server dynamically using jQuery in
sections "Ajax using jQuery" and "jQuery Ajax with JSON"
No we will see few of the jQuery features that are easy to learn and implement with ASP.NET.
jQuery Selectors
Selectors are most useful feature in jQuery, Selectors helps to select element(s) in an HTML
document.
jQuery has large number of options for selectors, you can select an element or array of elements
by its ID, tag name, class name, with particular attributes. There are lots options to traverse
through parent and children to find out an element.
Every selector returns an array of elements rather than single element; I feel this is a very helpful
feature in jQuery while working with jQuery.
$(".header") //will return an array containing elements which has class name = header
There are lots of such selectors available in jQuery to help developers. You can check out more
of them here: http://docs.jquery.com/Selectors
jQuery Chainability
It's a magical concept in jQuery to make code short and simple. Every jQuery method returns
query object itself so it will allow performing function on it and maintaining a chain.
Example:
$("div").addClass("sample").html("html changed").show();
This code will add class "sample" to all divs, will replace its inner HTML with "html changed"
and make all divs visible.
Example:
If we want to place JavaScript confirm message on click of delete link in a grid, assume this link
contains class "removeItem"
$(". removeItem").each{function()
{
$(this).bind("click",
return confirm("Are you sure you wish to delete this item?"));
}
jQuery Events
jQuery has great collection of event related helpers, you can check them out here
http://docs.jquery.com/Events. For event handling, bind and unbind is most usable methods.
Example:
$("p").bind("click", function()
{
$(this).append("clicked");
});
This code will add onclick event handler to all paragraphs in document and on click of them will
append text "clicked" to the clicked paragraph
function clickMe()
{
$("#debugDiv").append("clicked!");
}
jQuery has document ready event which helps to run methods when DOM is ready to be
traversed and manipulated.
$(document).ready(function()
{
$("p").append("This text appended on DOM ready");
});
This will append "This text appended on DOM ready" to paragraphs available in document when
DOM is ready.
"load" method is a simplest form of jQuery Ajax, It will load html content in document element
returned from server
There are lots of free code and libraries available for asp.net to generate JSON data. Json.Net is
one among them.
I've created simple method using Json.Net which will return JSON representation of a datatable.
Now we will try to parse this JSON data and build a grid out of this data at client side.
It is really simple task using jQuery:
$('#divSample').hide(1000); // will hide the div "divSample", and it will animate for 1000
miliseconds
$('#divSample').show(1000); // will show the div "divSample"
$('#divSample').toggle (1000); // will toggle display of the div "divSample"
jQuery allow us to create our own animation using "animate" method:
There are lots of thing in jQuery which I could not cover in this article, I recommend you to visit
http://jquery.com/ and http://docs.jquery.com/Main_Page to learn more about jQuery.