<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Peter Gerritsen&#039;s blog &#187; AJAX</title>
	<atom:link href="http://blog.petergerritsen.nl/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.petergerritsen.nl</link>
	<description>about .Net and SharePoint development</description>
	<lastBuildDate>Wed, 28 Jul 2010 08:28:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Building an AJAX web part with jQuery (Part 3)</title>
		<link>http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-3/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-3/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 13:36:35 +0000</pubDate>
		<dc:creator>Peter Gerritsen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MOSS]]></category>

		<guid isPermaLink="false">http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-3/</guid>
		<description><![CDATA[In part 1 of this series I explained a bit about the context and goal of creating an AJAX web part without using ASP.Net AJAX. I also showed the steps necessary for creating services that return data in the JSON format. In part 2 I showed you how to call these services from JavaScript and render the HTML for the [...]<p><a href="http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-3/">Building an AJAX web part with jQuery (Part 3)</a> is a post from: <a href="http://blog.petergerritsen.nl">Peter Gerritsen&#039;s blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In <a  href="http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/">part 1</a> of this series I explained a bit about the context and goal of creating an AJAX web part without using ASP.Net AJAX. I also showed the steps necessary for creating services that return data in the JSON format. In <a  href="http://blogs.tamtam.nl/peterg/2009/03/31/BuildingAnAJAXWebPartWithJQueryPart2.aspx" target="_blank">part 2</a> I showed you how to call these services from JavaScript and render the HTML for the data. In this last part I’ll show you how to use the jQuery UI and validation plugins.</p>
<h4>jQuery UI</h4>
<p>The <a  href="http://jqueryui.com/home" target="_blank">jQuery UI</a> plugin provides some useful widgets and effects to use in your jQuery based scripts. It also offers an advanced theme framework, so you don’t have to write all the css by yourself. You can use one of the included theme’s or roll your own with the <a  href="http://jqueryui.com/themeroller/" target="_blank">ThemeRoller</a>.</p>
<p>I’ve decided to use the UI plugin for tree things in my web part:</p>
<ul>
<li> Datepicker widget to specify the orderdate</li>
<li> Dialog widget to show confirmation dialogs, edit forms and validation messages</li>
<li> Highlighting effect to focus the users attention to changing data, such as the shoppingcart</li>
</ul>
<p><strong>Datepicker</strong></p>
<p>The datepicker enhances a standard text input box with a datapicker that slides out when the textbox receives focus. It contains different options for specifying the allowed dates, year/month selection and more. When it’s shown it will look like this:</p>
<p><a  href="http://blog.petergerritsen.nl/wp-content/uploads/snipping20.png" class="thickbox no_icon" rel="gallery-558" title="image"><img style="border-width: 0px; display: inline;" title="image" src="http://blog.petergerritsen.nl/wp-content/uploads/snipping21.png" border="0" alt="image" /></a></p>
<p>Linking it to your input box is very simple. I use the following code:</p>
<pre class="brush: javascript">
$(&quot;#bpvorderdate&quot;).datepicker({
showOn: &#039;button&#039;,
minDate: +1, dateFormat: &#039;dd/mm/yy&#039;,
buttonImage: &#039;/_layouts/images/calendar.gif&#039;,
buttonImageOnly: true
});
</pre>
<p>In this case, the user has to press a button (in this case an imagebutton) to open the datepicker.</p>
<p><strong>Dialogs</strong></p>
<p>Dialogs are a very useful way to give feedback to the user or asking for confirmation. In my web part I want to show a confirmation dialog when a user presses the delete icon next to a product in the shoppingcart or the “clear shoppingcart” button. The user will be presented with the following dialog:</p>
<p><a  href="http://blog.petergerritsen.nl/wp-content/uploads/snipping22.png" class="thickbox no_icon" rel="gallery-558" title="image"><img style="border-width: 0px; display: inline;" title="image" src="http://blog.petergerritsen.nl/wp-content/uploads/snipping23.png" border="0" alt="image" /></a></p>
<p>Showing this is very easy. First we create a function that is called when the page is initialized:</p>
<pre class="brush: javascript">
function initializeDeleteItemDialog() {
var doOk= function() {
var paramsdata = {
&quot;productId&quot; : $(&quot;#bpvremoveitemid&quot;).val()
}
$.ajax({
type: &quot;POST&quot;, url: &quot;/_layouts/intranet2009/bpvshoppingcart.asmx/DeleteItem&quot;,
data: JSON.stringify(paramsdata),
contentType: &quot;application/json;charset=utf-8&quot;,
dataType: &quot;json&quot;,
success:rendershoppingcart,
error: showError
});

$(&quot;#bpvremoveitemdialog&quot;).dialog(&quot;close&quot;);
}
var doCancel = function()
{
$(&quot;#bpvremoveitemdialog&quot;).dialog(&quot;close&quot;);
}
var dialogOpts = {
modal: true,
buttons: {&quot;Bewaren&quot;: doCancel, &quot;Verwijderen&quot;: doOk},
autoOpen: false
}
$(&quot;#bpvremoveitemdialog&quot;).dialog(dialogOpts);
}
</pre>
<p>We first specify the code to execute when the user presses the Ok button. In this case we’ll call the DeleteItem method of the shoppingcart web service and then close the dialog. The Cancel button will close the dialog straight away. In the dialog options we specify the buttons with their callback. Then we hook up the dialog to the html element we want to show. The html is written out in the Render method of the web part:</p>
<pre class="brush: javascript">
writer.WriteLine(“&lt;div id=\”bpvremoveitemdialog\” title=\”Product verwijderen?\”&gt;”);
writer.WriteLine(“Weet u zeker dat u dit product uit uw winkelwagen wilt verwijderen?”);
writer.WriteLine(“&lt;input type=\”hidden\” id=\”bpvremoveitemid\”/&gt;”);
writer.WriteLine(“&lt;/div&gt;”);
</pre>
<p>To open the dialog we just have to call the dialog method again with “open” as parameter:</p>
<pre class="brush: javascript">
function removeProduct(element) {
$(&quot;#bpvremoveitemid&quot;).val($(element).attr(&quot;productid&quot;) );
$(&quot;#bpvremoveitemdialog&quot;).dialog(&quot;open&quot;);
}
</pre>
<p><strong>Validation</strong></p>
<p>Validation of your inputs is supposed to be really easy with the <a  href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/" target="_blank">validation plugin</a>. Unfortunately this doesn’t count when you combine it with ASP.Net Webforms. With the validation plugin you attach the validation to a form within your html. Because ASP.Net Webforms uses one form tag for the entire page, this doesn’t allow you to set validation to a group of elements that would normally be contained within their own form tag. The solution I came up with for now only validates 1 element at a time.<br />
If you now of a way to assign one validation and remove it again before assigning a new validation, let me know.</p>
<p>First we hook up all the validations we want on the form and we specify a custom validation<br />
rule, called dutchDate:</p>
<pre class="brush: javascript">
$.validator.addMethod(
&quot;dutchDate&quot;, function(value,element)
{ return value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/);},
&quot;Voer een datum in van het formaat dd/mm/yyyy&quot; );
$(&quot;form&quot;).validate({
onsubmit: false,
onfocusout: false,
onkeyup: false,
onclick: false,
showErrors: showValidationError,
rules: {
bpvproductamount: {
required: true,
number: true
},
bpvproductid: {
required: true
},
bpvorderdate: {
required: true,
dutchDate: true
}
},
messages: {
bpvproductamount: {
required: &quot;Aantal is een verplicht veld&quot;,
number: &quot;Aantal moet een getal zijn&quot;
},
bpvproductid: {
required: &quot;U heeft geen product geselecteerd&quot; },
bpvorderdate: {
required: &quot;Bezorg-/ophaaldatum is een verplicht veld&quot;,
dutchDate: &quot;Bezorg-/ophaaldatum moet in het formaat dd/mm/yyyy zijn&quot;
}
}
});
</pre>
<p>I only want the validation to occur when I call it on specific elements from code, so we specify false on every event it normally triggers on. When there are errors,  I want to call a showValidationError function that shows the errors in a dialog box. Then we specify the rules and the messages we want to show when the rule isn’t matched. “bpvproductamount” equals the name attribute of the input element.</p>
<p>To call the validation we use the element method of the validation plugin:</p>
<pre class="brush: javascript">
if ($(&quot;form&quot;).validate().element(&quot;#txtbpvproductid&quot;) &amp;&amp; $(&quot;form&quot;).validate().element(&quot;#txtbpvproductamount&quot;))
{
// valid, so perfom actions
}
</pre>
<p>As soon as an element doesn’t pass validation, the method we attached to the showErrors event is called. Unfortunately this means only one error at a time will popup if multiple  elements don’t pass validation. To show the validation messages, we’ll make use of the Dialog widget once again:</p>
<pre class="brush: javascript">
function showValidationError(errorMap, errorList)
{
var message = &quot;&quot;;
var i;
for(i=0; i &lt; errorList.length; i++) {
message += errorList[i].message + &quot;&lt;br /&gt;&quot;;
}
if (message.length &gt; 0) {
showMessage(message);
}
}
</pre>
<h4>Conclusion</h4>
<p>Building an AJAX web part with jQuery (and some plugins) can result in a very responsive UI with a good user experience. In the end, I don’t think building a web part with ASP.Net AJAX would have taken me less time as well. I’m not happy with the validation though. Although the jQuery validation plugin is very useful in most web frameworks (including ASP.Net MVC), it seems that it doesn’t combine well with web forms. But I haven’t been able to find a better plugin for it.</p>
<!-- RO Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-3/" title="Add 'Building an AJAX web part with jQuery (Part 3)' to Twitter"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/twitter.png" title="Add 'Building an AJAX web part with jQuery (Part 3)' to Twitter" alt="Add 'Building an AJAX web part with jQuery (Part 3)' to Twitter" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.myspace.com/Modules/PostTo/Pages/?t=Building+an+AJAX+web+part+with+jQuery+%28Part+3%29&#038;c=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-3/" title="Add 'Building an AJAX web part with jQuery (Part 3)' to MySpace"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/myspace.png" title="Add 'Building an AJAX web part with jQuery (Part 3)' to MySpace" alt="Add 'Building an AJAX web part with jQuery (Part 3)' to MySpace" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-3/&#038;title=Building+an+AJAX+web+part+with+jQuery+%28Part+3%29" title="Add 'Building an AJAX web part with jQuery (Part 3)' to Del.icio.us"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/delicious.png" title="Add 'Building an AJAX web part with jQuery (Part 3)' to Del.icio.us" alt="Add 'Building an AJAX web part with jQuery (Part 3)' to Del.icio.us" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&#038;url=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-3/&#038;title=Building+an+AJAX+web+part+with+jQuery+%28Part+3%29" title="Add 'Building an AJAX web part with jQuery (Part 3)' to digg"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/digg.png" title="Add 'Building an AJAX web part with jQuery (Part 3)' to digg" alt="Add 'Building an AJAX web part with jQuery (Part 3)' to digg" /></a><br /><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-3/&#038;t=Building+an+AJAX+web+part+with+jQuery+%28Part+3%29" title="Add 'Building an AJAX web part with jQuery (Part 3)' to FaceBook"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/facebook.png" title="Add 'Building an AJAX web part with jQuery (Part 3)' to FaceBook" alt="Add 'Building an AJAX web part with jQuery (Part 3)' to FaceBook" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-3/" title="Add 'Building an AJAX web part with jQuery (Part 3)' to Technorati"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/technorati.png" title="Add 'Building an AJAX web part with jQuery (Part 3)' to Technorati" alt="Add 'Building an AJAX web part with jQuery (Part 3)' to Technorati" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-3/&#038;title=Building+an+AJAX+web+part+with+jQuery+%28Part+3%29" title="Add 'Building an AJAX web part with jQuery (Part 3)' to Stumble Upon"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/stumbleupon.png" title="Add 'Building an AJAX web part with jQuery (Part 3)' to Stumble Upon" alt="Add 'Building an AJAX web part with jQuery (Part 3)' to Stumble Upon" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&#038;output=popup&#038;bkmk=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-3/&#038;title=Building+an+AJAX+web+part+with+jQuery+%28Part+3%29" title="Add 'Building an AJAX web part with jQuery (Part 3)' to Google Bookmarks"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/google.png" title="Add 'Building an AJAX web part with jQuery (Part 3)' to Google Bookmarks" alt="Add 'Building an AJAX web part with jQuery (Part 3)' to Google Bookmarks" /></a></div>
<!-- RO Social Bookmarks END --><p><a  href="http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-3/">Building an AJAX web part with jQuery (Part 3)</a> is a post from: <a  href="http://blog.petergerritsen.nl">Peter Gerritsen&#039;s blog</a></p>
<div class="fblike" style="height:25px; height:25px; overflow:hidden;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.petergerritsen.nl%2F2009%2F03%2F31%2Fbuilding-an-ajax-web-part-with-jquery-part-3%2F&amp;layout=standard&amp;show_faces=false&amp;width=320&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allow Transparency="true" style="border:none; overflow:hidden; width:320px;"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Building an AJAX web part with jQuery (Part 2)</title>
		<link>http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-2/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-2/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 10:29:31 +0000</pubDate>
		<dc:creator>Peter Gerritsen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MOSS]]></category>

		<guid isPermaLink="false">http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-2/</guid>
		<description><![CDATA[In part 1 of this series I explained a bit about the context and goal of creating an AJAX web part without using ASP.Net AJAX. I also showed the steps necessary for creating services that return data in the JSON format. In this post I’ll show you how to call these services from JavaScript and insert the data in [...]<p><a href="http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-2/">Building an AJAX web part with jQuery (Part 2)</a> is a post from: <a href="http://blog.petergerritsen.nl">Peter Gerritsen&#039;s blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In <a  href="http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/" target="_blank">part 1</a> of this series I explained a bit about the context and goal of creating an AJAX web part without using ASP.Net AJAX. I also showed the steps necessary for creating services that return data in the JSON format. In this post I’ll show you how to call these services from JavaScript and insert the data in the HTML placeholders rendered by the web part.</p>
<p><strong>Calling the generic handler for products and categories</strong></p>
<p>jQuery offers various methods to perform asynchronous calls to web resources. To retrieve JSON the most used are jQuery.ajax and jQuery.getJSON. The last one uses a HTTP GET request and is simpler to use, the jQuery.ajax method offers more options/flexibility. For retrieving the product categories and the products, I’ve decided to go with getJSON.<br />
The code for retreiving the categories looks as follows:</p>
<pre class="brush: javascript">
function showProductCategories() {
$.getJSON(bpvweburl + &quot;/_layouts/intranet2009/bpv.ashx&quot;,
{type: &quot;categories&quot;},
function(data)
{
var categoriescontainer = $(&quot;#bpvcategoriescontainer&quot;);
categoriescontainer.empty();
var list = categoriescontainer.append($(&quot;#bpvcategorytemplate&quot;).html());
var directive = {&#039;a.context[onclick]&#039; : &#039;&quot;showProducts(this);return false;&quot;&#039;};
list.autoRender( data, directive );
});
}
</pre>
<p>The first line is responsible for calling the handler. It specifies a inline callback method to handle the returned data. The JSON returned is processed by the PURE templating plugin. I’ve blogged about using this <a  href="http://blog.petergerritsen.nl/2009/03/13/using-a-template-plugin-for-jquery-to-parse-json-data/" target="_blank">before</a>.</p>
<p><strong>Calling the shopping cart web service</strong></p>
<p>For getting the data from the web service, we’ll use the jQuery.ajax method. That’s because we need to do a HTTP POST request as well as specify some other options for the request. For more information see <a  href="http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/" target="_blank">this</a> post by Dave Ward. To initially load the shopping cart we use the following code:</p>
<pre class="brush: javascript">
function loadShoppingcart()
{
$.ajax({
type: &quot;POST&quot;,
url: &quot;/_layouts/ecabointranet2009/bpvshoppingcart.asmx/GetItems&quot;,
data: &quot;{}&quot;,
contentType: &quot;application/json; charset=utf-8&quot;,
dataType: &quot;json&quot;,
success: rendershoppingcart,
error: showError
});
}

function rendershoppingcart(msg) {
var cartcontainer = $(&quot;#bpvcartcontent&quot;);
cartcontainer.empty();

if (msg.d.length &gt; 0)
{
var cartitemslist = cartcontainer.append($(&quot;#bpvcarttemplate&quot;).html());

var directives = {
&#039;a.bpvedit[onclick]&#039; : &#039;&quot;editAmount(this); return false;&quot;&#039;,
&#039;a.bpvremove[onclick]&#039; : &#039;&quot;removeProduct(this); return false;&quot;&#039;
}
cartitemslist.autoRender( msg.d, directives );
}
else
{
cartcontainer.append(&quot;&lt;span&gt;U heeft nog geen producten in uw winkelwagen&lt;/span&gt;&quot;);

}

$(&quot;#bpvcartcontent&quot;).effect(&quot;highlight&quot;, {color: &quot;#ffcf57&quot;}, 700, null);
}

function showError(xhr, status, error)
{
var err = eval(&quot;(&quot; + xhr.responseText + &quot;)&quot;);

$(&quot;#bpverrordialog span.errormessage&quot;).html(err.Message);

$(&quot;#bpverrordialog&quot;).dialog(&quot;open&quot;);
}
</pre>
<p>As you can see we post to the GetItems method of the asmx. We need to specify a empty JSON object as data (more information on that in <a  href="http://encosia.com/2008/06/05/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax/" target="_blank">this</a> post, again by Dave Ward) and specify the contentType we want returned. When the AJAX call returns an error, we’ll call the showError method (which uses the jQuery UI dialog widget I’ll tell more about in part 3). When the call is successful, the rendershoppingcart method is called. This checks if the cart is empty, so we can display a message in that case, or uses PURE again for rendering the cart contents. To provide visual feedback to the user when the cart is updated, we’ll use the highlight effect on the cart to attract the attention of the user.<br />
If we want to pass in parameters with an AJAX call (like the productId when we want to delete an item from the cart), we need to construct a parameters object and serialize that as string for usage in the call:</p>
<pre class="brush: javascript">
var paramsdata = { &quot;productId&quot; : $(&quot;#bpvremoveitemid&quot;).val() }

$.ajax({
type: &quot;POST&quot;,
url: &quot;/_layouts/ecabointranet2009/bpvshoppingcart.asmx/DeleteItem&quot;,
data: JSON.stringify(paramsdata),
contentType: &quot;application/json; charset=utf-8&quot;,
dataType: &quot;json&quot;,
success: rendershoppingcart,
error: showError
});
</pre>
<p>As you can see, we use a JSON.stringify method for serializing the object as a string.</p>
<p>You can <a  href="http://www.json.org/json2.js" target="_blank">download</a> the scriptfile needed for this from JSON.org.</p>
<p>All the other methods in the web service are called in the same way, so there’s no need to inundate you with more code on that. The only thing left is to show you parts of the contents of the Render method in the webpart:</p>
<pre class="brush: csharp">
// Templates
// Categorylist template
writer.WriteLine(&quot;&lt;div id=\&quot;bpvcategorytemplate\&quot; style=\&quot;display: none;\&quot;&gt;&quot;);
writer.WriteLine(&quot;&lt;h3&gt;Productcategorie&lt;/h3&gt;&quot;);
writer.WriteLine(&quot;&lt;ul&gt;&lt;li class=\&quot;context\&quot;&gt;&lt;a href=\&quot;#\&quot; class=\&quot;context context@category\&quot;&gt;laden...&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&quot;);
writer.WriteLine(&quot;&lt;/div&gt;&quot;);
// Productlist template
writer.WriteLine(&quot;&lt;div id=\&quot;bpvproducttemplate\&quot; style=\&quot;display: none;\&quot;&gt;&quot;);
writer.WriteLine(&quot;&lt;h3&gt;Product&lt;/h3&gt;&quot;);
writer.WriteLine(&quot;&lt;ul&gt;&lt;li class=\&quot;context\&quot;&gt;&lt;a href=\&quot;#\&quot; class=\&quot;Title ID@productid Description@description Code@productcode AttachmentUrl@imageurl\&quot;&gt;geen items&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&quot;);
writer.WriteLine(&quot;&lt;/div&gt;&quot;);
// Shoppingcart template
writer.WriteLine(&quot;&lt;div id=\&quot;bpvcarttemplate\&quot; style=\&quot;display: none;\&quot;&gt;&quot;);
writer.WriteLine(&quot;&lt;h3&gt;Informatie en bestellen&lt;/h3&gt;&quot;);
writer.WriteLine(&quot;&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;td class=\&quot;ProductName\&quot;&gt;Product&lt;/td&gt;&lt;td&gt;Aantal&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody class=\&quot;d\&quot;&gt;&quot;);
writer.WriteLine(&quot;&lt;tr class=\&quot;context\&quot;&gt;&lt;td class=\&quot;ProductName\&quot;&gt;naam&lt;/td&gt;&lt;td class=\&quot;Amount\&quot;&gt;aantal&lt;/td&gt;&lt;td&gt;&lt;a href=\&quot;#\&quot; class=\&quot;bpvedit ProductID@productid Amount@amount\&quot;&gt;&lt;img src=\&quot;/_layouts/images/ecabo/2009/page-edit.gif\&quot; /&gt;&lt;/a&gt; &lt;a href=\&quot;#\&quot; class=\&quot;bpvremove ProductID@productid\&quot;&gt;&lt;img src=\&quot;/_layouts/images/ecabo/2009/bin.gif\&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&quot;);
writer.WriteLine(&quot;&lt;/tbody&gt;&lt;/table&gt;&quot;);
writer.WriteLine(&quot;&lt;/div&gt;&quot;);

// Item selector
writer.WriteLine(&quot;&lt;div id=\&quot;bpvitemselector\&quot;&gt;&quot;);
writer.WriteLine(&quot;&lt;h2&gt;1. Selecteer uw producten&lt;/h2&gt;&quot;);
writer.WriteLine(&quot;&lt;div id=\&quot;selector\&quot;&gt;&quot;);
writer.WriteLine(&quot;&lt;div id=\&quot;bpvcategoriescontainer\&quot;&gt;&lt;h3&gt;Productcategorie&lt;/h3&gt;&lt;/div&gt;&quot;);
writer.WriteLine(&quot;&lt;div id=\&quot;bpvproductscontainer\&quot;&gt;&lt;h3&gt;Product&lt;/h3&gt;&lt;/div&gt;&quot;);
writer.WriteLine(&quot;&lt;/div&gt;&quot;);
writer.WriteLine(&quot;&lt;div id=\&quot;bpvproductinfo\&quot;&gt;&lt;h3&gt;Informatie en bestellen&lt;/h3&gt;&quot;);
writer.WriteLine(&quot;&lt;img src=\&quot;/_layouts/images/blank.gif\&quot; id=\&quot;bpvproductimage\&quot;/&gt;&quot;);
writer.WriteLine(&quot;&lt;span class=\&quot;title\&quot; id=\&quot;bpvproducttitle\&quot;&gt;&lt;/span&gt;&quot;);
writer.WriteLine(&quot;&lt;span class=\&quot;description\&quot;  id=\&quot;bpvproductdescription\&quot; /&gt;&lt;/span&gt;&quot;);
writer.WriteLine(&quot;&lt;label class=\&quot;amount\&quot;&gt;Aantal:&lt;/label&gt;&lt;input type=\&quot;text\&quot; id=\&quot;txtbpvproductamount\&quot; name=\&quot;bpvproductamount\&quot;/&gt;&quot;);
writer.WriteLine(&quot;&lt;input type=\&quot;hidden\&quot; id=\&quot;txtbpvproductid\&quot; name=\&quot;bpvproductid\&quot;/&gt;&quot;);
writer.WriteLine(&quot;&lt;input type=\&quot;hidden\&quot; id=\&quot;txtbpvproductcode\&quot; /&gt;&quot;);
writer.WriteLine(&quot;&lt;input type=\&quot;button\&quot; id=\&quot;bpvaddproduct\&quot; value=\&quot;Voeg toe\&quot;/&gt;&quot;);
writer.WriteLine(&quot;&lt;/div&gt;&quot;);
writer.WriteLine(&quot;&lt;/div&gt;&quot;);

// Shoppingcart
writer.WriteLine(&quot;&lt;div id=\&quot;bpvshoppingcart\&quot;&gt;&quot;);
writer.WriteLine(&quot;&lt;h2&gt;2. Lijst met uw bestelling&lt;/h2&gt;&quot;);
writer.WriteLine(&quot;&lt;div id=\&quot;bpvcartcontent\&quot;&gt;&lt;span&gt;U heeft nog geen producten in uw winkelwagen&lt;/span&gt;&lt;/div&gt;&quot;);

writer.WriteLine(&quot;&lt;a id=\&quot;clearcart\&quot; href=\&quot;#\&quot; onclick=\&quot;clearCart(); return false;\&quot;&gt;Alles verwijderen&lt;/a&gt;&quot;);
writer.WriteLine(&quot;&lt;/div&gt;&quot;);
</pre>
<p>As you can see, all we do is write out HTML. First I write out the HTML needed for the databinding of the categories and products (I removed that because it’s the same as the category one). Then some placeholders and form elements are rendered. There’s a little more HTML off course, but you get the point, NO CODE <img src='http://blog.petergerritsen.nl/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>In the <a  href="http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-3/" target="_blank">next part</a> I’ll show you how to use some jQuery plugins to enhance the experience of the user and validation of the input.</p>
<!-- RO Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-2/" title="Add 'Building an AJAX web part with jQuery (Part 2)' to Twitter"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/twitter.png" title="Add 'Building an AJAX web part with jQuery (Part 2)' to Twitter" alt="Add 'Building an AJAX web part with jQuery (Part 2)' to Twitter" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.myspace.com/Modules/PostTo/Pages/?t=Building+an+AJAX+web+part+with+jQuery+%28Part+2%29&#038;c=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-2/" title="Add 'Building an AJAX web part with jQuery (Part 2)' to MySpace"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/myspace.png" title="Add 'Building an AJAX web part with jQuery (Part 2)' to MySpace" alt="Add 'Building an AJAX web part with jQuery (Part 2)' to MySpace" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-2/&#038;title=Building+an+AJAX+web+part+with+jQuery+%28Part+2%29" title="Add 'Building an AJAX web part with jQuery (Part 2)' to Del.icio.us"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/delicious.png" title="Add 'Building an AJAX web part with jQuery (Part 2)' to Del.icio.us" alt="Add 'Building an AJAX web part with jQuery (Part 2)' to Del.icio.us" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&#038;url=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-2/&#038;title=Building+an+AJAX+web+part+with+jQuery+%28Part+2%29" title="Add 'Building an AJAX web part with jQuery (Part 2)' to digg"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/digg.png" title="Add 'Building an AJAX web part with jQuery (Part 2)' to digg" alt="Add 'Building an AJAX web part with jQuery (Part 2)' to digg" /></a><br /><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-2/&#038;t=Building+an+AJAX+web+part+with+jQuery+%28Part+2%29" title="Add 'Building an AJAX web part with jQuery (Part 2)' to FaceBook"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/facebook.png" title="Add 'Building an AJAX web part with jQuery (Part 2)' to FaceBook" alt="Add 'Building an AJAX web part with jQuery (Part 2)' to FaceBook" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-2/" title="Add 'Building an AJAX web part with jQuery (Part 2)' to Technorati"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/technorati.png" title="Add 'Building an AJAX web part with jQuery (Part 2)' to Technorati" alt="Add 'Building an AJAX web part with jQuery (Part 2)' to Technorati" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-2/&#038;title=Building+an+AJAX+web+part+with+jQuery+%28Part+2%29" title="Add 'Building an AJAX web part with jQuery (Part 2)' to Stumble Upon"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/stumbleupon.png" title="Add 'Building an AJAX web part with jQuery (Part 2)' to Stumble Upon" alt="Add 'Building an AJAX web part with jQuery (Part 2)' to Stumble Upon" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&#038;output=popup&#038;bkmk=http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-2/&#038;title=Building+an+AJAX+web+part+with+jQuery+%28Part+2%29" title="Add 'Building an AJAX web part with jQuery (Part 2)' to Google Bookmarks"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/google.png" title="Add 'Building an AJAX web part with jQuery (Part 2)' to Google Bookmarks" alt="Add 'Building an AJAX web part with jQuery (Part 2)' to Google Bookmarks" /></a></div>
<!-- RO Social Bookmarks END --><p><a  href="http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-2/">Building an AJAX web part with jQuery (Part 2)</a> is a post from: <a  href="http://blog.petergerritsen.nl">Peter Gerritsen&#039;s blog</a></p>
<div class="fblike" style="height:25px; height:25px; overflow:hidden;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.petergerritsen.nl%2F2009%2F03%2F31%2Fbuilding-an-ajax-web-part-with-jquery-part-2%2F&amp;layout=standard&amp;show_faces=false&amp;width=320&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allow Transparency="true" style="border:none; overflow:hidden; width:320px;"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Building an AJAX web part with jQuery (Part 1)</title>
		<link>http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 10:08:51 +0000</pubDate>
		<dc:creator>Peter Gerritsen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MOSS]]></category>

		<guid isPermaLink="false">http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/</guid>
		<description><![CDATA[In this series of posts I will describe how I build a web part full of AJAX functionality using jQuery and some plugins for jQuery. Why? ASP.Net AJAX is quite hard to implement using only code. Besides that, having multiple updatepanels and multiple triggers outside of these updatepanels, can complicate stuff very quickly. So I decided to see [...]<p><a href="http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/">Building an AJAX web part with jQuery (Part 1)</a> is a post from: <a href="http://blog.petergerritsen.nl">Peter Gerritsen&#039;s blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In this series of posts I will describe how I build a web part full of AJAX functionality using jQuery and some plugins for jQuery.</p>
<h4>Why?</h4>
<p>ASP.Net AJAX is quite hard to implement using only code. Besides that, having multiple updatepanels and multiple triggers outside of these updatepanels, can complicate stuff very quickly. So I decided to see how much time I would need to build the required functionality using JavaScript with jQuery.</p>
<p><strong>What?</strong></p>
<p>The web part of choice was one that would allow visitors of the portal to order a bunch of products from different categories. Products are stored in a list and have a choice sitecolumn to specify the category of the item. Products can be selected and added to a shopping cart. After the wanted items are added to the shopping cart, the user can specify some comments, a handling method (Pick-up or deliver) and a delivery-/pickup date and send in the order. The order information is saved in a list, the user gets a confirmation email and the shopping cart is cleared. The resulting web part look like this (sorry for the dutch interface):</p>
<p><a  href="http://blog.petergerritsen.nl/wp-content/uploads/snipping24.png" class="thickbox no_icon" rel="gallery-562" title="image"><img style="display: inline; border-width: 0px;" title="image" src="http://blog.petergerritsen.nl/wp-content/uploads/snipping25.png" border="0" alt="image" /></a></p>
<p><strong>How?</strong></p>
<p>For getting the categories and products we’ll use a generic handler. This offers the possibility of caching and is perfectly suitable for read-only data access. The shopping cart and order functionality will be provided through a ASP.Net Webservice. Both the handler and web service will return JSON data. We’ll need ASP.Net 3.5 for this.</p>
<p>The web part will only override the render method and write out the needed HTML. So no control instantiation, event handling or other code.</p>
<p><strong>Useful links</strong></p>
<p>Dave Ward’s weblog on <a  href="http://encosia.com/" target="_blank">encosia.com</a> provides a vital source for information on combining jQuery and ASP.Net. Also <a  href="http://www.west-wind.com/Weblog/" target="_blank">Rick Strahl</a> has some useful posts on this subject.</p>
<p><strong>Building the products handler</strong></p>
<p>We’ll start by building the Generic Handler that returns JSON data for categories and products. We’ll differentiate between the two by passing in the type with the query string. I’ve created two classes for data retrieval from the list in SharePoint, one that returns the choices of the categories and one that returns the products based on the category. We’ll then use the DataContractJsonSerializer to generate and return the JSON for this data:</p>
<pre class="brush: csharp">
public void ProcessRequest(HttpContext context)
{
try
{
if (string.IsNullOrEmpty(context.Request[&quot;type&quot;]))
throw new ArgumentException(&quot;type not specified or null&quot;);

string type = context.Request[&quot;type&quot;];

context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(300));
context.Response.Cache.SetCacheability(HttpCacheability.Public);

if (type.Equals(&quot;categories&quot;, StringComparison.InvariantCultureIgnoreCase))
{
StringCollection categories = BPVProductCategory.GetProductCategories();

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(StringCollection));
ser.WriteObject(context.Response.OutputStream, categories);
}
if (type.Equals(&quot;products&quot;, StringComparison.InvariantCultureIgnoreCase))
{
if (string.IsNullOrEmpty(context.Request[&quot;category&quot;]))
throw new ArgumentException(&quot;category not specified or null&quot;);

string category = HttpUtility.UrlDecode(context.Request[&quot;category&quot;]);

Ecabo.Intranet2009.SharePoint.Diagnostics.Logging.LogMessageFormat(&quot;Category: {0}&quot;, category);

List&lt;bpvproduct&gt; products = BPVProduct.GetAvailableProducts(category);

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List&lt;bpvproduct&gt;));
ser.WriteObject(context.Response.OutputStream, products);
}
}
catch (Exception ex)
{
context.Response.Write(string.Format(&quot;ERROR: {0}&quot;, ex.Message));
}
}
</pre>
<h4>Building the shopping cart web service</h4>
<p>Next, we need to create a web service that will allows us to modify the shopping cart with add, remove, change and clear methods. This web service will also contain a method to place the order. To make sure this web service will return JSON when requested, we’ll decorate the class with the ScriptService attribute (normally you just have to comment out the automatically included line in the class definition) and we decorate<br />
the methods with the ScriptMethod attribute in which we specify the ResponseFormat to be JSON. To store the shoppingcart between requests to the service we’ll use the Session. In order for that to work we add the EnableSession=true parameter to the WebMethod attribute of each method. The resulting code looks like this:</p>
<pre class="brush: csharp">
[WebService(Namespace = &quot;http://sharepoint.ecabo.nl/200903/&quot;)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class BPVShoppingCart : System.Web.Services.WebService
{
private List&lt;shoppingCartItem&gt; ShoppingCart
{
get
{
if (HttpContext.Current.Session[&quot;BPVShoppingCart&quot;] != null)
{
return (List&lt;shoppingCartItem&gt;)HttpContext.Current.Session[&quot;BPVShoppingCart&quot;];
}
else
{
List&lt;shoppingCartItem&gt; shoppingCart = new List&lt;shoppingCartItem&gt;();
HttpContext.Current.Session.Add(&quot;BPVShoppingCart&quot;, shoppingCart);
return shoppingCart;
}
}
set
{
HttpContext.Current.Session[&quot;BPVShoppingCart&quot;] = value;
}
}

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List&lt;shoppingCartItem&gt; GetItems()
{
try
{
return ShoppingCart;
}
catch (Exception ex)
{
Ecabo.Intranet2009.SharePoint.Diagnostics.Logging.LogException(ex);
return null;
}
}

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List&lt;shoppingCartItem&gt; AddItem(string productName, int productId, string productCode, int amount)
{
try
{
ShoppingCartItem item = ShoppingCart.Find(p =&gt; p.ProductID == productId);
if (item == null)
{
item = new ShoppingCartItem();
item.Amount = amount;
item.ProductCode = productCode;
item.ProductID = productId;
item.ProductName = productName;
ShoppingCart.Add(item);
}
else
{
item.Amount += amount;
}

return ShoppingCart;
}
catch (Exception ex)
{
Ecabo.Intranet2009.SharePoint.Diagnostics.Logging.LogException(ex);
return null;
}
}

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List&lt;shoppingCartItem&gt; DeleteItem(int productId)
{
try
{
ShoppingCartItem item = ShoppingCart.Find(p =&gt; p.ProductID == productId);
if (item != null)
{
ShoppingCart.Remove(item);
}

return ShoppingCart;
}
catch (Exception ex)
{
Ecabo.Intranet2009.SharePoint.Diagnostics.Logging.LogException(ex);
return null;
}
}

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List&lt;shoppingCartItem&gt; ChangeAmount(int productId, int amount)
{
try
{
if (amount == 0)
return DeleteItem(productId);

ShoppingCartItem item = ShoppingCart.Find(p =&gt; p.ProductID == productId);
if (item != null)
{
item.Amount = amount;
}

return ShoppingCart;
}
catch (Exception ex)
{
Ecabo.Intranet2009.SharePoint.Diagnostics.Logging.LogException(ex);
return null;
}
}

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List&lt;shoppingCartItem&gt; ClearCart()
{
try
{
ShoppingCart.Clear();
return ShoppingCart;
}
catch (Exception ex)
{
Ecabo.Intranet2009.SharePoint.Diagnostics.Logging.LogException(ex);
return null;
}
}

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List&lt;shoppingCartItem&gt; PlaceOrder(string comments, string deliveryType, string deliveryDate)
{
try
{
string products = &quot;&quot;;

foreach (ShoppingCartItem item in ShoppingCart)
{
products += string.Format(&quot;{0} - {1} - {2}\r\n&quot;, item.ProductCode, item.Amount, item.ProductName);
}

BPVBestelling bestelling = new BPVBestelling();
bestelling.Comments = comments;
bestelling.Handling = deliveryType;
bestelling.HandlingDate = Convert.ToDateTime(deliveryDate, new CultureInfo(&quot;nl-NL&quot;));
bestelling.Title = DateTime.Now.ToString(&quot;yyyyMMdd_HHmm&quot;) + &quot;_&quot; + SPContext.Current.Web.CurrentUser.Email;
bestelling.Products = products;
bestelling.PlacedBy = SPContext.Current.Web.CurrentUser;

if (BPVBestelling.AddBestelling(bestelling))
{
BPVBestelling.SendConfirmationEmail(bestelling, ShoppingCart);
ShoppingCart.Clear();
return ShoppingCart;
}
else
throw new Exception(&quot;Het is niet mogelijk om uw bestelling te verwerken&quot;);
}
catch (Exception ex)
{
Ecabo.Intranet2009.SharePoint.Diagnostics.Logging.LogException(ex);
throw new Exception(&quot;Het is niet mogelijk om uw bestelling te verwerken&quot;, ex);
}
}
}
</pre>
<p>There’s one more thing needed for letting the web service return the data in JSON format. We need to include a httpHandler in the web.config for the asmx extension that routes the request to the ScriptHandlerFactory. An easy way to do this is for your SharePoint webapp by creating a blank web.config and placing it in a subfolder of the layouts directory where you also place the asmx file. The following is all you need in that web.config file:</p>
<pre class="brush: xml">
&lt;?xml version=&quot;1.0&quot; standalone=&quot;yes&quot;?&gt;
&lt;configuration&gt;
&lt;system.web&gt;
&lt;httpHandlers&gt;
&lt;add verb=&quot;*&quot; path=&quot;*.asmx&quot; validate=&quot;false&quot; type=&quot;System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;/&gt;
&lt;/httpHandlers&gt;
&lt;compilation debug=&quot;true&quot;/&gt;&lt;/system.web&gt;
&lt;/configuration&gt;
</pre>
<p><strong>Next parts</strong></p>
<p>That wraps it up for this first post in the series. In <a  href="http://blog.petergerritsen.nl/2009/03/31/building-an-ajax-web-part-with-jquery-part-2/" target="_blank">part 2</a> I’ll show you how to call these services from jQuery and insert the data in the HTML of the webpart. In <a  href="http://blog.petergerritsen.nl/2009/05/16/what-asp-net-developers-should-know-about-jquery/" target="_blank">part 3</a> we’ll enhance the experience by including dialogs and validation in the solution.</p>
<!-- RO Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/" title="Add 'Building an AJAX web part with jQuery (Part 1)' to Twitter"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/twitter.png" title="Add 'Building an AJAX web part with jQuery (Part 1)' to Twitter" alt="Add 'Building an AJAX web part with jQuery (Part 1)' to Twitter" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.myspace.com/Modules/PostTo/Pages/?t=Building+an+AJAX+web+part+with+jQuery+%28Part+1%29&#038;c=http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/" title="Add 'Building an AJAX web part with jQuery (Part 1)' to MySpace"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/myspace.png" title="Add 'Building an AJAX web part with jQuery (Part 1)' to MySpace" alt="Add 'Building an AJAX web part with jQuery (Part 1)' to MySpace" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/&#038;title=Building+an+AJAX+web+part+with+jQuery+%28Part+1%29" title="Add 'Building an AJAX web part with jQuery (Part 1)' to Del.icio.us"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/delicious.png" title="Add 'Building an AJAX web part with jQuery (Part 1)' to Del.icio.us" alt="Add 'Building an AJAX web part with jQuery (Part 1)' to Del.icio.us" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&#038;url=http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/&#038;title=Building+an+AJAX+web+part+with+jQuery+%28Part+1%29" title="Add 'Building an AJAX web part with jQuery (Part 1)' to digg"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/digg.png" title="Add 'Building an AJAX web part with jQuery (Part 1)' to digg" alt="Add 'Building an AJAX web part with jQuery (Part 1)' to digg" /></a><br /><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/&#038;t=Building+an+AJAX+web+part+with+jQuery+%28Part+1%29" title="Add 'Building an AJAX web part with jQuery (Part 1)' to FaceBook"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/facebook.png" title="Add 'Building an AJAX web part with jQuery (Part 1)' to FaceBook" alt="Add 'Building an AJAX web part with jQuery (Part 1)' to FaceBook" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/" title="Add 'Building an AJAX web part with jQuery (Part 1)' to Technorati"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/technorati.png" title="Add 'Building an AJAX web part with jQuery (Part 1)' to Technorati" alt="Add 'Building an AJAX web part with jQuery (Part 1)' to Technorati" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/&#038;title=Building+an+AJAX+web+part+with+jQuery+%28Part+1%29" title="Add 'Building an AJAX web part with jQuery (Part 1)' to Stumble Upon"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/stumbleupon.png" title="Add 'Building an AJAX web part with jQuery (Part 1)' to Stumble Upon" alt="Add 'Building an AJAX web part with jQuery (Part 1)' to Stumble Upon" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&#038;output=popup&#038;bkmk=http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/&#038;title=Building+an+AJAX+web+part+with+jQuery+%28Part+1%29" title="Add 'Building an AJAX web part with jQuery (Part 1)' to Google Bookmarks"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/google.png" title="Add 'Building an AJAX web part with jQuery (Part 1)' to Google Bookmarks" alt="Add 'Building an AJAX web part with jQuery (Part 1)' to Google Bookmarks" /></a></div>
<!-- RO Social Bookmarks END --><p><a  href="http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/">Building an AJAX web part with jQuery (Part 1)</a> is a post from: <a  href="http://blog.petergerritsen.nl">Peter Gerritsen&#039;s blog</a></p>
<div class="fblike" style="height:25px; height:25px; overflow:hidden;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.petergerritsen.nl%2F2009%2F03%2F30%2Fbuilding-an-ajax-web-part-with-jquery-part-1%2F&amp;layout=standard&amp;show_faces=false&amp;width=320&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allow Transparency="true" style="border:none; overflow:hidden; width:320px;"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://blog.petergerritsen.nl/2009/03/30/building-an-ajax-web-part-with-jquery-part-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using a template plugin for jQuery to parse JSON data</title>
		<link>http://blog.petergerritsen.nl/2009/03/13/using-a-template-plugin-for-jquery-to-parse-json-data/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://blog.petergerritsen.nl/2009/03/13/using-a-template-plugin-for-jquery-to-parse-json-data/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 11:47:50 +0000</pubDate>
		<dc:creator>Peter Gerritsen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://blog.petergerritsen.nl/2009/03/13/using-a-template-plugin-for-jquery-to-parse-json-data/</guid>
		<description><![CDATA[When you’re building an AJAX control in .Net there a a few possibilities. One of them is using AJAX.Net updatepanels. This saves you from writing tedious javascript code to refresh parts of you page. With the arrival of javascript libraries such as jQuery it’s much easier to create the AJAX functionality you want with javascript. However, you still have [...]<p><a href="http://blog.petergerritsen.nl/2009/03/13/using-a-template-plugin-for-jquery-to-parse-json-data/">Using a template plugin for jQuery to parse JSON data</a> is a post from: <a href="http://blog.petergerritsen.nl">Peter Gerritsen&#039;s blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>When you’re building an AJAX control in .Net there a a few possibilities. One of them is using AJAX.Net updatepanels. This saves you from writing tedious javascript code to refresh parts of you page. With the arrival of javascript libraries such as jQuery it’s much easier to create the AJAX functionality you want with javascript. However, you still have to write quite a lot of DOM manipulation code and use string concatenation<br />
to process any JSON results and render the correct HTML.</p>
<p>Fortunately some javascript template engines are developed to make this easier. These engines come in all shapes and sizes, ranging from engines with an own templating syntax to simple data binding engines.</p>
<p>On the first side of the spectrum, there are engines such as <a  href="http://jtemplates.tpython.com/" target="_blank">jTemplates</a>, this one uses python like syntax to create the instructions. On the other end engines like <a  href="http://wiki.github.com/raid-ox/chain.js" target="_blank">Chain.js</a> and <a  href="http://beebole.com/pure/" target="_blank">PURE</a> live, these can be considered more a databinding egines. The last ones make use of classnames for the databinding, like in the following Chain.js example:</p>
<pre class="brush: html">
&lt;div id=&quot;quickdemo&quot;&gt;
	&lt;div class=&quot;item&quot;&gt;
		&lt;span class=&quot;library&quot;&gt;Library Name&lt;/span&gt;
	&lt;/div&gt;
&lt;/div&gt;
</pre>
<pre class="brush: javascript">
$(&#039;#quickdemo&#039;).items( [
		{library:&#039;Prototype&#039;},
		{library:&#039;jQuery&#039;},
		{library:&#039;Dojo&#039;},
		{library:&#039;MooTools&#039;}
	]).chain();
</pre>
<p>In this case the library field in the JSON objects is put into the element with “library” in de classname. The nice thing about Chain.js is the fact that it monitors the items collection for changes. Adding or removing items from script, automatically updates the generated HTML. So filtering and sorting can be very easily accomplished, some very easy to follow examples are available on the companion website.</p>
<p>PURE uses the same classnames based system for the databinding. Consider the following example:</p>
<pre class="brush: html">
&lt;ol class=&quot;siteList reference@id&quot;&gt;
	&lt;li class=&quot;sites&quot;&gt;
		&lt;a class=&quot;name url@href&quot; href=&quot;http://beebole.com&quot;&gt;Beebole&lt;/a&gt;
	&lt;/li&gt;
&lt;/ol&gt;
</pre>
<pre class="brush: javascript">
var data = {
	&quot;reference&quot;: &quot;3456&quot;,
	&quot;sites&quot;: [{
			&quot;name&quot;: &quot;Beebole&quot;,
			&quot;url&quot;: &quot;http://beebole.com&quot;
		},
		{
			&quot;name&quot;: &quot;BeeLit&quot;,
			&quot;url&quot;: &quot;http://beeLit.com&quot;
		},
		{
			&quot;name&quot;: &quot;PURE&quot;,
			&quot;url&quot;: http://beebole.com/pure
		}]
	};
	$(&#039;ol.siteList&#039;).autoRender(data);
</pre>
<p>The <em>url@href</em> and <em>reference@id</em> classnames provide a way to set attributes of the databound elements.</p>
<p>But what if you don’t want to decorate your HTML elements with extra classnames to support the databinding? Or you want to handle some events of the generated elements?</p>
<p>For this PURE supports directives. You create your directives and pass them into the autoRender or render functions. Consider the following example:</p>
<pre class="brush: html">
&lt;div style=&quot;display: none;&quot; id=&quot;bpvcategorytemplate&quot;&gt;
	&lt;ul&gt;
		&lt;li class=&quot;context&quot;&gt;
			&lt;a category=&quot;&quot; class=&quot;context context@category&quot; href=&quot;#&quot;&gt;laden...&lt;/a&gt;
		&lt;/li&gt;
	&lt;/ul&gt;
&lt;/div&gt;
</pre>
<pre class="brush: javascript">
function showProductCategories()
{
    $.getJSON(bpvweburl + &quot;/_layouts/ecabointranet2009/bpv.ashx&quot;, {type: &quot;categories&quot;}, function(data)
    {
        var categoriescontainer = $(&quot;#bpvcategoriescontainer&quot;);
        categoriescontainer.empty();

        var list = categoriescontainer.append($(&quot;#bpvcategorytemplate&quot;).html());

        var directive = {&#039;a.context[onclick]&#039; : &#039;&quot;showProducts(this); return false;&quot;&#039;}

        list.autoRender( data, directive );
    });
}
</pre>
<p>Here we get some JSON from a handler, put the html from the template into a new element, and bind the JSON to that template. When binding the JSON data, we attach a javascript function to the onclick event of the generated anchor tags.</p>
<p>Much more can be accomplished by using directives, such as creating an alternating row style by setting a class during the binding of the items. For more information about using directives in PURE, read <a  href="http://wiki.github.com/pure/pure/what-is-a-directive" target="_blank">this</a> page.</p>
<p>There are loads more things possible in PURE or in the other engines, the best way to find out is to read the docs and try some things out.</p>
<!-- RO Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://blog.petergerritsen.nl/2009/03/13/using-a-template-plugin-for-jquery-to-parse-json-data/" title="Add 'Using a template plugin for jQuery to parse JSON data' to Twitter"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/twitter.png" title="Add 'Using a template plugin for jQuery to parse JSON data' to Twitter" alt="Add 'Using a template plugin for jQuery to parse JSON data' to Twitter" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.myspace.com/Modules/PostTo/Pages/?t=Using+a+template+plugin+for+jQuery+to+parse+JSON+data&#038;c=http://blog.petergerritsen.nl/2009/03/13/using-a-template-plugin-for-jquery-to-parse-json-data/" title="Add 'Using a template plugin for jQuery to parse JSON data' to MySpace"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/myspace.png" title="Add 'Using a template plugin for jQuery to parse JSON data' to MySpace" alt="Add 'Using a template plugin for jQuery to parse JSON data' to MySpace" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://blog.petergerritsen.nl/2009/03/13/using-a-template-plugin-for-jquery-to-parse-json-data/&#038;title=Using+a+template+plugin+for+jQuery+to+parse+JSON+data" title="Add 'Using a template plugin for jQuery to parse JSON data' to Del.icio.us"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/delicious.png" title="Add 'Using a template plugin for jQuery to parse JSON data' to Del.icio.us" alt="Add 'Using a template plugin for jQuery to parse JSON data' to Del.icio.us" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&#038;url=http://blog.petergerritsen.nl/2009/03/13/using-a-template-plugin-for-jquery-to-parse-json-data/&#038;title=Using+a+template+plugin+for+jQuery+to+parse+JSON+data" title="Add 'Using a template plugin for jQuery to parse JSON data' to digg"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/digg.png" title="Add 'Using a template plugin for jQuery to parse JSON data' to digg" alt="Add 'Using a template plugin for jQuery to parse JSON data' to digg" /></a><br /><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://blog.petergerritsen.nl/2009/03/13/using-a-template-plugin-for-jquery-to-parse-json-data/&#038;t=Using+a+template+plugin+for+jQuery+to+parse+JSON+data" title="Add 'Using a template plugin for jQuery to parse JSON data' to FaceBook"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/facebook.png" title="Add 'Using a template plugin for jQuery to parse JSON data' to FaceBook" alt="Add 'Using a template plugin for jQuery to parse JSON data' to FaceBook" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://blog.petergerritsen.nl/2009/03/13/using-a-template-plugin-for-jquery-to-parse-json-data/" title="Add 'Using a template plugin for jQuery to parse JSON data' to Technorati"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/technorati.png" title="Add 'Using a template plugin for jQuery to parse JSON data' to Technorati" alt="Add 'Using a template plugin for jQuery to parse JSON data' to Technorati" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://blog.petergerritsen.nl/2009/03/13/using-a-template-plugin-for-jquery-to-parse-json-data/&#038;title=Using+a+template+plugin+for+jQuery+to+parse+JSON+data" title="Add 'Using a template plugin for jQuery to parse JSON data' to Stumble Upon"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/stumbleupon.png" title="Add 'Using a template plugin for jQuery to parse JSON data' to Stumble Upon" alt="Add 'Using a template plugin for jQuery to parse JSON data' to Stumble Upon" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&#038;output=popup&#038;bkmk=http://blog.petergerritsen.nl/2009/03/13/using-a-template-plugin-for-jquery-to-parse-json-data/&#038;title=Using+a+template+plugin+for+jQuery+to+parse+JSON+data" title="Add 'Using a template plugin for jQuery to parse JSON data' to Google Bookmarks"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/google.png" title="Add 'Using a template plugin for jQuery to parse JSON data' to Google Bookmarks" alt="Add 'Using a template plugin for jQuery to parse JSON data' to Google Bookmarks" /></a></div>
<!-- RO Social Bookmarks END --><p><a  href="http://blog.petergerritsen.nl/2009/03/13/using-a-template-plugin-for-jquery-to-parse-json-data/">Using a template plugin for jQuery to parse JSON data</a> is a post from: <a  href="http://blog.petergerritsen.nl">Peter Gerritsen&#039;s blog</a></p>
<div class="fblike" style="height:25px; height:25px; overflow:hidden;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.petergerritsen.nl%2F2009%2F03%2F13%2Fusing-a-template-plugin-for-jquery-to-parse-json-data%2F&amp;layout=standard&amp;show_faces=false&amp;width=320&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allow Transparency="true" style="border:none; overflow:hidden; width:320px;"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://blog.petergerritsen.nl/2009/03/13/using-a-template-plugin-for-jquery-to-parse-json-data/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adding AJAX.Net to your MOSS WebParts</title>
		<link>http://blog.petergerritsen.nl/2009/02/18/adding-ajax-net-to-your-moss-webparts/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=rss</link>
		<comments>http://blog.petergerritsen.nl/2009/02/18/adding-ajax-net-to-your-moss-webparts/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 12:53:37 +0000</pubDate>
		<dc:creator>Peter Gerritsen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[MOSS]]></category>

		<guid isPermaLink="false">http://blog.petergerritsen.nl/2009/02/18/adding-ajax-net-to-your-moss-webparts/</guid>
		<description><![CDATA[I know there are loads of posts on this subject already, but in this one I’ll try to give some useful tips on this subject. First, you need to add AJAX.Net entries to your web.config of the MOSS site. A very easy way to do this is by using the Ajaxify stsadm extensions. Simply add the included [...]<p><a href="http://blog.petergerritsen.nl/2009/02/18/adding-ajax-net-to-your-moss-webparts/">Adding AJAX.Net to your MOSS WebParts</a> is a post from: <a href="http://blog.petergerritsen.nl">Peter Gerritsen&#039;s blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I know there are loads of posts on this subject already, but in this one I’ll try to give some useful tips on this subject.</p>
<p>First, you need to add AJAX.Net entries to your web.config of the MOSS site. A very easy way to do this is by using the <a  href="http://www.codeplex.com/ajaxifymoss" target="_blank">Ajaxify</a> <a  href="http://www.codeplex.com/ajaxifymoss" target="_blank">stsadm extensions</a>.</p>
<p>Simply add the included wsp to your solutions and deploy. After that you can easily add the entries by running the <em>stsadm –addajaxmoss</em> command. Unfortunately you need to add one entry manually, but that is given as part of the output when you run the command.</p>
<p>Next you need to add a ScriptManager to the pages with the AJAX enabled webparts. You can off course modify the masterpage. I chose to create a base web part that checks if a ScriptManager has been added to the page and adds it if necessary:</p>
<pre class="brush: csharp">
protected override void OnInit(EventArgs e)
{
base.OnInit(e);

//Register the ScriptManager

ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);

if (scriptManager == null)
{
scriptManager = new ScriptManager();
scriptManager.ID = “ScriptManager1″;

scriptManager.EnablePartialRendering = true;

this.Page.Form.Controls.AddAt(0, scriptManager);
}
}
</pre>
<p>Because MOSS needs a lot of JavaScript to function properly, this unfortunately requires another fix. You can read more about it <a  href="http://msdn.microsoft.com/en-us/library/bb861877.aspx" target="_blank">here</a>. I added the fix to the base webpart with the following code:</p>
<pre class="brush: csharp">
protected override void CreateChildControls()
{
//Add fix according to http://msdn2.microsoft.com/en-us/library/bb861877.aspx

EnsurePanelFix();
}

private void EnsurePanelFix()
{
if (this.Page.Form!= null)
{
String fixupScript = @”
_spBodyOnLoadFunctionNames.push(“”_initFormActionAjax”&quot;);
function _initFormActionAjax()
{
if (_spEscapedFormAction == document.forms[0].action)
{
document.forms[0]._initialAction = document.forms[0].action;
}
}

var RestoreToOriginalFormActionCore = RestoreToOriginalFormAction;
RestoreToOriginalFormAction = function()
{
if (_spOriginalFormAction != null)
{
RestoreToOriginalFormActionCore();
document.forms[0]._initialAction = document.forms[0].action;
}
}”;

ScriptManager.RegisterStartupScript(this, typeof(BaseWebPart), “UpdatePanelFixup”, fixupScript, true);
}
}
</pre>
<p>Because all the content you want to include in an UpdatePanel needs to be added to the ControlTemplateContainer.Controls collection, it’s not possible to write out any html you need between controls in the UpdatePanel by using the standard HtmlTextWriter.WriteLine method in the Render method. The easiest way is to add LiteralControls to the controlcollection of the UpdatePanel (with thanks to my collegue Wouter Lemaire for giving me the tip). To make this even easier, I’ve added the following extension method to my project:</p>
<pre class="brush: csharp">
public static void AddLiteral(this UpdatePanel updatePanel, string html)
{
Literal lit = new Literal();
lit.Text = html + “\r\n”;
updatePanel.ContentTemplateContainer.Controls.Add(lit);
}
</pre>
<p>You can call this method from the CreateChildControls method in your web part like so:</p>
<pre class="brush: csharp">
updatePanel.AddLiteral(“&lt;table&gt;&lt;tr&gt;”);
updatePanel.AddLiteral(“&lt;td&gt;&lt;/td&gt;&lt;td&gt;Ma&lt;/td&gt;&lt;td&gt;Di&lt;/td&gt;&lt;td&gt;Wo&lt;/td&gt;&lt;td&gt;Do&lt;/td&gt;&lt;td&gt;Vr&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;”);
updatePanel.AddLiteral(“&lt;td&gt;Ochtend&lt;/td&gt;&lt;td&gt;”);
</pre>
<p>To indicate progress you need some form of visual feedback to the user. For this you need to set the ProgressTemplate of the UpdatePanel. For this you need to create a class that implements the ITemplate interface.</p>
<p>Then you implement the InstantiateIn method to create a template:</p>
<pre class="brush: csharp">
public void ITemplate.InstantiateIn(Control container)
{
Label lbl = new Label();
lbl.Text = “Progress….”;
container.Controls.Add(lbl);
}
</pre>
<p>Then add an object of this class to the ProgressTemplate property of the UpdatePanel:</p>
<pre class="brush: csharp">
updateProgress.ProgressTemplate = new ProgressTemplate();
</pre>
<!-- RO Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home?status=http://blog.petergerritsen.nl/2009/02/18/adding-ajax-net-to-your-moss-webparts/" title="Add 'Adding AJAX.Net to your MOSS WebParts' to Twitter"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/twitter.png" title="Add 'Adding AJAX.Net to your MOSS WebParts' to Twitter" alt="Add 'Adding AJAX.Net to your MOSS WebParts' to Twitter" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.myspace.com/Modules/PostTo/Pages/?t=Adding+AJAX.Net+to+your+MOSS+WebParts&#038;c=http://blog.petergerritsen.nl/2009/02/18/adding-ajax-net-to-your-moss-webparts/" title="Add 'Adding AJAX.Net to your MOSS WebParts' to MySpace"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/myspace.png" title="Add 'Adding AJAX.Net to your MOSS WebParts' to MySpace" alt="Add 'Adding AJAX.Net to your MOSS WebParts' to MySpace" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://blog.petergerritsen.nl/2009/02/18/adding-ajax-net-to-your-moss-webparts/&#038;title=Adding+AJAX.Net+to+your+MOSS+WebParts" title="Add 'Adding AJAX.Net to your MOSS WebParts' to Del.icio.us"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/delicious.png" title="Add 'Adding AJAX.Net to your MOSS WebParts' to Del.icio.us" alt="Add 'Adding AJAX.Net to your MOSS WebParts' to Del.icio.us" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&#038;url=http://blog.petergerritsen.nl/2009/02/18/adding-ajax-net-to-your-moss-webparts/&#038;title=Adding+AJAX.Net+to+your+MOSS+WebParts" title="Add 'Adding AJAX.Net to your MOSS WebParts' to digg"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/digg.png" title="Add 'Adding AJAX.Net to your MOSS WebParts' to digg" alt="Add 'Adding AJAX.Net to your MOSS WebParts' to digg" /></a><br /><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://blog.petergerritsen.nl/2009/02/18/adding-ajax-net-to-your-moss-webparts/&#038;t=Adding+AJAX.Net+to+your+MOSS+WebParts" title="Add 'Adding AJAX.Net to your MOSS WebParts' to FaceBook"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/facebook.png" title="Add 'Adding AJAX.Net to your MOSS WebParts' to FaceBook" alt="Add 'Adding AJAX.Net to your MOSS WebParts' to FaceBook" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://blog.petergerritsen.nl/2009/02/18/adding-ajax-net-to-your-moss-webparts/" title="Add 'Adding AJAX.Net to your MOSS WebParts' to Technorati"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/technorati.png" title="Add 'Adding AJAX.Net to your MOSS WebParts' to Technorati" alt="Add 'Adding AJAX.Net to your MOSS WebParts' to Technorati" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://blog.petergerritsen.nl/2009/02/18/adding-ajax-net-to-your-moss-webparts/&#038;title=Adding+AJAX.Net+to+your+MOSS+WebParts" title="Add 'Adding AJAX.Net to your MOSS WebParts' to Stumble Upon"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/stumbleupon.png" title="Add 'Adding AJAX.Net to your MOSS WebParts' to Stumble Upon" alt="Add 'Adding AJAX.Net to your MOSS WebParts' to Stumble Upon" /></a><a  class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&#038;output=popup&#038;bkmk=http://blog.petergerritsen.nl/2009/02/18/adding-ajax-net-to-your-moss-webparts/&#038;title=Adding+AJAX.Net+to+your+MOSS+WebParts" title="Add 'Adding AJAX.Net to your MOSS WebParts' to Google Bookmarks"><img src="http://blog.petergerritsen.nl/wp-content/plugins/ro-social-bookmarks/google.png" title="Add 'Adding AJAX.Net to your MOSS WebParts' to Google Bookmarks" alt="Add 'Adding AJAX.Net to your MOSS WebParts' to Google Bookmarks" /></a></div>
<!-- RO Social Bookmarks END --><p><a  href="http://blog.petergerritsen.nl/2009/02/18/adding-ajax-net-to-your-moss-webparts/">Adding AJAX.Net to your MOSS WebParts</a> is a post from: <a  href="http://blog.petergerritsen.nl">Peter Gerritsen&#039;s blog</a></p>
<div class="fblike" style="height:25px; height:25px; overflow:hidden;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fblog.petergerritsen.nl%2F2009%2F02%2F18%2Fadding-ajax-net-to-your-moss-webparts%2F&amp;layout=standard&amp;show_faces=false&amp;width=320&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allow Transparency="true" style="border:none; overflow:hidden; width:320px;"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://blog.petergerritsen.nl/2009/02/18/adding-ajax-net-to-your-moss-webparts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
