Showing posts with label jQuery. Show all posts
Showing posts with label jQuery. Show all posts

Thursday, 7 May 2009

Calling server side ASP.NET method from jQuery

I was recently looking at calling a server side method from an ASP.NET 3.5 application using jQuery. The following example has a server side method that returns some HTML markup from a database to jQuery, which then inserts it into the page markup.

Server side code, in Default.aspx:


using System.Web.Services;

[WebMethod(true)]
>(public static string getMarkup()
{
return "<h3>Test Markup</h3>";
}


You need to make the method static and include the WebMethod(true) operator above it. You can also use the following code to call a WebMethod from an asmx or WCF web service.

You call this from the client side jQuery code using:


$("#loadHome").click(function(){
$.ajax({
type: "POST",
url: "./Default.aspx/getMarkup",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result){
$("#contentDiv").html(result.d);
}
});
});


This uses the jQuery ajax method to get the data from the server side method, which is returned as a JSON object. You use the .d notation on the JSON object to get the result supplied from the server side.

If you want to send any data to the method, send as a JSON object. For example if you had a server side method that takes 2 params, orderId and customerId, you would use:

var data = "{orderId:'2',customerId:'99'}";

Monday, 27 April 2009

jQuery events not firing for dynamic content

When adding dynamic content (using AJAX or DOM modification) using jQuery, events are not fired for the new objects, for example if you have the following html:

<button id="load" href="#">Test</button>
<div id="container"></div>


When I click the load button I want to add a link to another page inside the container DIV, so I use the following jQuery.


$(function() {
$("#load").click(function() {
$("#container").html("<a id='popup' href='contactForm.html'>popup</a>")
});

$("#popup").click(function(){
alert("Clicked");
return false;
});
});


The click event for the dynamically created anchor tag (id="popup") will not fire using the above because this code is run once the document is ready, and at that time jQuery will look for an object with an Id of popup which doesn't exist yet.

I've found a few different ways to do this:

1. Add a generic click event for the body tag and check what was clicked, for example:


$("body").click(function(e){
var target = $(e.target);
if (target.is("#popup")) {
alert("Clicked");
return false;
}
});


Using this example you check what was clicked using target.is(), you can use any valid CSS selector statements to check the item clicked. This way is simple to implement but can quickly grow if you have a lot of dynamic content.

2. Use the live command, which will tell jQuery to bind any current and future references of "#popup" to the click event.



$("#popup").live("click",function(){
alert("Clicked");
return false;
});



The following 3 ways are variations on the same theme in that the events are bound after content creation.

3. Use the jQuery Bind command to bind the click event to a particular function on creation. e.g.:


$("#load").click(function(){
$("#container").html("<a id='popup' href='contactForm.html'>popup</a>").bind('click', loadClick);
//can also use
//$("#container").html("<a id='popup' href='contactForm.html'>popup</a>").click(loadClick);
});

function loadClick(){
alert("Clicked");
return false;
}


4. Insert the function for handling the click event within the initial click event when you create the popup link. This runs the event binding as soon as the item is added.


$("#load").click(function(){
$("#container").html("<a id='popup' href='contactForm.html'>popup</a>");
$("#popup").click(function(){
alert("Clicked");
return false;
});
});


5. Similar to the above method except this way you create a method which does all data binding (bindEvents()). This can be useful if you have a lot dynamic content that needs to be bound at the same time, it can be encapsulated into 1 event.


$("#load").click(function(){
$("#container").html("<a id='popup' href='contactForm.html'>popup</a>");
bindEvents();
});

function bindEvents(){
$("#popup").click(function(){
alert("Clicked");
return false;
});
};



There's also a plugin called livequery that makes binding easier.

Wednesday, 25 March 2009

jQuery

I've been looking at using Javascript libraries recently, in order to revamp Lesley's site there are a lot out there (Prototype, Mootools, scriptaculous, Dojo, YUI). I've decided to use JQuery as it appears to have the largest following, which means more support, tutorials and extra plugins. It's also easily extensible meaning you can customise and extend.

What is it? "jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript." In short, you can get Flash-type effects and general all-round sexiness using only javascript, could be used for cool menus, client side table sorting, tabs, AJAX data/image loading, page manipulation, custom dialog boxes, charts etc.

To use you need some understanding of CSS as it uses CSS-style selectors ("li", "#Id", ".class", "a>img" etc) to access elements using jQuery and it appears a lot of the functionality uses CSS to move/position elements.

Here are some good links that I've found:
Free Hit Counter