14
jQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

JQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

Embed Size (px)

Citation preview

Page 1: JQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

jQuery and ASP.NETBetter together for building rich

web apps

Fritz OnionCo-Founder, Pluralsight

Page 2: JQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

Introduction

• jQuery– Has become the de facto standard for

client-side programming– Being actively added to by MS Ajax team

• ASP.NET Ajax– Effectively deprecated at this point– Ajax Toolkit still available, but prefer jQuery

• ASP.NET + jQuery–Work well together

Page 3: JQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

Adding jQuery to ASP.NET

• Using NuGet– Can add most any version / configuration

using NuGet from within VS2010

• Downloading directly from jQuery.com– Can download the package you want and

copy script files locally

• Generating jQuery UI custom packages– Special feature of jQueryUI – generate

custom ‘themed’ packages

Page 4: JQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

Toolkit Controls

• Controls available in the Ajax Toolkit

Accordion ControlAlwaysVisible ControlAnimation ControlAsyncFileUpload ControlAutoComplete ControlCalendar ControlCascadingDropdown ControlCollapsiblePanel ControlColorPicker ControlComboBox ControlConfirmButton ControlDragPanel ControlDropDown ControlDropShadow ControlDynamicPopulate Control

FilteredTextBox ControlHoverMenu ControlHTMLEditor ControlListSearch ControlMaskedEdit ControlModalPopup ControlMultiHandleSlider ControlMutuallyExclusiveCheckBox ControlNoBot ControlNumericUpDown ControlPagingBulletedList ControlPasswordStrength ControlPopup ControlRating Control

ReorderList ControlResizable ControlRoundedCorners ControlSeadragon ControlSlider ControlSlideShow ControlTabContainer and TabPanel ControlsTextBoxWatermark ControlToggleButton ControlUpdatePanelAnimation ControlValidatorCallout Control

Page 5: JQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

jQuery.UI

• Widgets available in jQuery.UI

Page 6: JQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

jQuery and WebForms

• ClientID management– Use ClientIdMode=xxx

• Augment server-side controls with client behaviors

<asp:Label ClientIDMode="Static" ID="myId" Text="hi" runat="server" />

<span id="myId">hi</span>

Renders as

Page 7: JQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

ClientIdMode

• ClientIDMode lets you control client ids for server controls– AutoID – same as 3.5x and earlier– Static – what you type is what's used– Predictable – concatenates ID properties

of naming containers (which you can set with new properties)• ClientIDRowSuffix

– Inherit (default) – whatever the immediate parent specifies

Page 8: JQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

Using jQuery and MVC

• Injecting jQuery scripts

• Leveraging jQuery features in MVC apps

<head> <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script></head>

Page 9: JQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

Server Communication with jQuery

• $.ajax()• Calling the server• JSON serialization• jQuery data rendering• jQuery templates

Page 10: JQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

Server Communication

• High level options– load()– get()– post()– getJSON()

• Low level options– ajax()

$(function() { $("a").click(getInstructorInfo);}

function getInstructorInfo(event) { var instructorName = $(this).text(); $("#detail").load("getInstructorInfo.ashx", { "instructor": instructorName }); return true;}

Page 11: JQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

Using get() and post()

• Allows access to raw XmlHTTPRequest object

• Packages data into query string (GET) or form values (POST)

• Callback function invoked to process result– Only invoked on successful completion

function getInstructorInfo(event) {  var instructorName = $(this).text(); $.post("getInstructorInfo.ashx", // url { "instructor" : instructorName }, // data function(result) { $("#detail").html(result) }, // callback "html"); // type of data}

Page 12: JQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

Error Handling

• Need to use lower level ajax() method or ajaxError() method.

• With ajax() method, single parameter specifies all options– Includes success and error callback functions

var instructorName = "Dr. Evil";$.ajax( { type: "POST", url: "getInstructorInfo.ashx", data: { "instructor": instructorName }, timeout: 5000, // ms success: function(result) { $("#detail").html(result) }, error: function(xhr, status, exception) {

$("#detail").html( "There was an error.<br/> " + "Status: " + status + "<br/>" + "XHR Status: " + xhr.statusText + "<br/>"); } } );

Page 13: JQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

Summary

• jQuery and jQuery.UI– Huge community support (including MS)– Continue to be refined and added to

• ASP.NET Ajax– No more active work being done– Generally prefer jQuery moving forward

Page 14: JQuery and ASP.NET Better together for building rich web apps Fritz Onion Co-Founder, Pluralsight

© 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.

The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and

Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.