Lesson2 Jquery With ASP
Lesson2 Jquery With ASP
net
With the object model, JavaScript gets all the power it needs to create
dynamic HTML:
What is JQuery?
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.
Userful Selectors Examples:
$("#selectDiv") //will return an array containing element which has id = selectDiv
$("div") //will return an array containing all "div" elements.
$(".header") //will return an array containing elements which has class name = header
$("input[name='emailaddress']") //will return an array containing "input" elements which
has name = emailaddress
:first-child $("p:first-child") All <p> elements that are the first child of their
parent
:first-of-type $("p:first-of-type") All <p> elements that are the first <p> element
of their parent
:last-child $("p:last-child") All <p> elements that are the last child of their
parent
:last-of-type $("p:last-of-type") All <p> elements that are the last <p> element
of their parent
:nth-child(n) $("p:nth-child(2)") All <p> elements that are the 2nd child of their
parent
:nth-last-child(n) $("p:nth-last-child(2)") All <p> elements that are the 2nd child of their
parent, counting from the last child
:nth-of-type(n) $("p:nth-of-type(2)") All <p> elements that are the 2nd <p> element
of their parent
:nth-last-of-type(n) $("p:nth-last-of-type(2)") All <p> elements that are the 2nd <p> element
of their parent, counting from the last child
:only-child $("p:only-child") All <p> elements that are the only child of their
parent
:only-of-type $("p:only-of-type") All <p> elements that are the only child, of its
type, of their parent
parent > child $("div > p") All <p> elements that are a direct child of a
<div> element
parent descendant $("div p") All <p> elements that are descendants of a
<div> element
element + next $("div + p") The <p> element that are next to each <div>
elements
element ~ siblings $("div ~ p") All <p> elements that are siblings of a <div>
element
:header $(":header") All header elements <h1>, <h2> ...
[attribute] $("[href]") All elements with a href attribute
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 2012 and 2015 include jQuery by
default and provide intellisense to use jQuery.
jQuery simplifies HTML document traversing, event handling, animating, and AJAX
interactions for rapid web development.The major benefits of using JQuery for latest
web applications is that it is very lightweight JavaScript library as its minified version
is just few kilobytes in size which means it loads faster on the client side
Above line adds JQuery library reference to your web application, in simple terms
with above line you web application understands JQuery syntax and work
accordingly.
Step 4: Put following line of code in you content page inside <asp:Content>.
Below line will display "Hello World" alert when page loads.
<script language="javascript" type="text/javascript">
$(document).ready(function () {
alert("Hello World");
});
</script>
Understanding $(document).ready(function ()
If you want an JQuery event to work on your page, you should call it inside the $
(document).ready() function. Everything inside it will load as soon as the DOM is
loaded and before the page contents are loaded.
And for that reason you may find that most of JQuery code lies within
});
</script>
So finally you will be able to display alert on page load in your asp.net web
application with JQuery.
May be this question might comes to your mind, I can show "Hello
World" alert without using JQuery then what is fun of using JQuery?
Well its too early to judge, since this article is for beginners i have explained with
simple example there is lot more coming. Please refer to my other upcoming
JQuery tutorials.
Once you have downloaded JQuery you are ready to play with it. Create a Website
project in Visual Studio and add JQuery script file in Scripts folder as shown below:
Next you need to add the JQuery script file reference in your web page. You can drag
the file from solution explorer and drop it inside the <head> tag in your page.
Once you have JQuery file reference added to the page, you can do so many magical
things on you page but for the purpose of this tutorial and to show you a simple
example I am creating two ASP.NET button controls and a Panel control on the page.
Clicking "Show Panel" button will make the panel visible with JQuery slide down
effect and clicking the "Hide Panel" button will hide the panel with JQuery slide up
effect.
Hello World!
</asp:Panel>
In order to tell the browser to perform some action using JQuery as soon as the
document is ready or loaded, you need to add the following script block in
the <head>section of your page. JQuery ready method specifies a function to execute
when the DOM is fully loaded and constructed by the browser and usually the best
place to attach all other event handlers and run other JQuery code.
<script type="text/javascript">
$(document).ready(function() {
// add JQuery or JavaScript code here
});
</script>
Inside the ready method handler function, first I am creating some variables to access
ASP.NET server controls by their client ids.
<script type="text/javascript">
$(document).ready(function() {
});
</script>
If you are using Master Pages and your controls are inside Content pages or inside
any other container such as Panel or Placeholder control then ASP.NET generates
client Ids which are different then their correspondent server ids. So you may not be
able to access your server side controls with the typical JQuery selector mentioned in
the above code example. In that case you can use any one of the following techniques:
$("[id$='_Button1']");
You can read more about these techniques at one of my HOW TOs available at "How
to Access ASP.NET Controls client id in JQuery".
<script type="text/javascript">
$(document).ready(function() {
var Button1 = $("#Button1");
var Button2 = $("#Button2");
var Panel1 = $("#Panel1");
Button1.click(function (e) {
Panel1.slideDown();
e.preventDefault();
});
Button2.click(function (e) {
Panel1.slideUp();
e.preventDefault();
});
});
</script>
Build and run your project and you will see Panel control sliding up and down with
smooth animation effect.
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).
In the above code, I have not used "http" protocol while referencing jQuery
from Google CDN
After this setup, you can use jQuery in your ASP.NET page. Let's see a demo.
<script type="text/javascript">
$(document).ready(function() {
$("#btnSubmit").click(function() {
});
});
</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.
$('#<%=Label1.ClientID%>').text();
$('#<%=Label1.ClientID%>').text("New Value");
$('#<%=TextBox1.ClientID%>').val();
$('#<%=TextBox1.ClientID%>').val("New Value");
Get Dropdown value:
$('#<%=DropDownList1.ClientID%>').val();
$('#<%=DropDownList1.ClientID%>').val("New Value");
$('#<%=DropDownList1.ClientID%> option:selected').text();
$('#<%=CheckBox1.ClientID%>').attr('checked');
$('#<%=CheckBox1.ClientID%>').attr('checked',true);
$('#<%=CheckBox1.ClientID%>').attr('checked',false);
$('#<%=RadioButton1.ClientID%>').attr('checked');
$('#<%=RadioButton1.ClientID%>').attr('checked',true);
$('#<%=RadioButton1.ClientID%>').attr('checked',false);
$('#<%=TextBox1.ClientID%>').attr('disabled', true);
$('#<%=TextBox1.ClientID%>').attr('disabled', false);
Make textbox read only:
$('#<%=TextBox1.ClientID%>').attr('readonly', 'readonly');
he jQuery UI DatePicker is probably the most widely-used widgets, yet the most under
utilized. It comes with a huge number of configurable options and a range of utility
functions that you can use to customize the widget the way you want.
The DatePicker has an easy to use interface for selecting date. It is tied to a simple text
field which when focused on by clicking or by using tab key, presents an interactive
calendar like interface to the user, allowing a date to be selected.
Create a new file ‘datepicker.html’ in a folder. Here’s how to write the HTML code and
call the datepicker() method in the <script> tag.
<html>
<head>
<title>DatePicker Example</title>
<link href="../css/jquery-ui.css" rel="stylesheet" />
<script src="../scripts/jquery-1.11.1.min.js"></script>
<script src="../scripts/jquery-ui.min.js"></script>
<script>
$(function () {
$("#datep").datepicker();
});
</script>
</head>
<body>
<h3>Click to select a date :</h3>
<input type="text" id="datep" />
</body>
</html>
Here we are using an <input> element with id=date and binding
the datepicker() widget to this input text field. The datepicker will automatically be
positioned below the input field when a user clicks it. The calendar will be hidden when
the user clicks outside
. If the user chooses a
date, the date will then be displayed in the input field:
Live Demo: http://www.jquerycookbook.com/demos/S9-General/67-datepicker.html
jQuery UI Datepicker offer a large set of options and methods. For a full list,
see: http://api.jqueryui.com/datepicker/
Note: Do not use the HTML5 type="date" attribute in your <input> statements when
using jQuery UI DatePicker as it causes conflicts
Similar to the previous example, we can disable the weekend days using the
before ShowDay option by passing a shorthand function $.datepicker. no
Weekends that automatically disables the week end days without any other
code needed.
<script type="text/javascript">
$(function () {
$("#datepicker").datepicker({
beforeShowDay: $.datepicker.noWeekends
});
});
</script>
View the page in the browser and you will observe that the weekends are
disabled.
Live Demo: http://www.jquerycookbook.com/demos/S9-General/67.2-datepicker.html