Listing RecipejsFile
//Preference name="MicrosoftAjax.js" function getDataUsing]avaScriptAjaxAsync() {
///<summary>This method makes a web request to obtain an XML file</summary>
var req = new Sys.Net.WebRequest();
req.set_url("http://localhost:9090/xml/ApressBooks.xml");
req.set_httpVerb("GET");
req.set_userContext("user's context");
req.add_completed(OnWebRequestCompleted);
function OnWebRequestCompleted(executor, eventArgs) { if (executor.get_responseAvailable()) {
var xmlString = executor.get_responseData();
//Call Managed Code method to pass back data - Covered in Recipe 6.6 document.getElementById("Xamll").Content.Page.SetBookXMLData(xmlString);
Although this book does not focus onASP.NETAJAX, we will cover the highlights of Listing 6-10. The firstline (with the <reference name..> element commented out with three slashes) is the equivalent of a using in C#, and it brings in the MicrosoftAJAX Library script file. We use the Sys.Net.WebRequest JavaScript class to make theweb request asynchronously. When getDataUsinglavaScriptAjaxAsync is invokedin the Silverlight application's UpdateDataButton_ Click event handler, the data is not immediately returned to the Silverlight application when this event handler executes:
private void UpdateDataButton_Click(object sender, RoutedEventArgs e) {
HtmlPage.Window.Invoke("getDataUsingHavaScriptAjaxAsync");
Instead, the web request asynchronously retrieves the data, which is returned via the OnWe-bRequestCompleted JavaScript method. The OnWebRequestCompleted JavaScript method shown in Listing 6-10 invokes a scriptable method called SetBookXMLData() located in the Silverlight application in order to return the data. A scriptable method is a managed code method that is made available in JavaScript. We cover how to call managed code in Recipe 6-6 in detail.
To summarize, when the Silverlight Update Data Button is clicked, the event handler invokes the JavaScript method getDatallsingHavaScriptAjaxAsync to initiate the asynchronous call and immediatelyreturns—that is, the UI thread is notblocking. When the ApressBooks.xml data is returned to the browser, the OnWebRequestCompleted JavaScript method passes the data back to Silverlight, where it is parsed by the class named ApressBooks using Linq to XML. We cover this Linq to XML functionality in Recipe 2-9, so we don't show the listing here. The only difference is that the data binding that happens inXAML without any C# code in Recipe 2-9 is now handled via this method in the Page class code-behind file:
[ScriptableMember]
public void SetBookXMLData(string data) {
ApressBooks books = new ApressBooks(data); Binding b = new Binding("ApressBookList"); b.Source = books.ApressBookList; BookListBox.ItemsSource = books.ApressBookList;
Figure 6-8 shows the final UI when the button is clicked.
Test Page for Recipe 6.5 - Windows Internet Explore lala I £3 r
I ^ http:/ localhost:i'jJj/Ree ipe6.5TestP ag e.aspx
* X If? LiveSeardi
Silverlight 2 has technology for interacting between the SttverEght 2 managed code and the hosting browser's HTML DOM called the HTML Bridge The HTML Bridge enables developers to expose entire managed code types or just member functions of types for scripting in JavaScript Developers can enable or disable HTML Bridge functionality' by setting the enableHtmlAccess parameter on the Silverlight browser plug-in to a Boolean value, with the default as false or disabled.
Pro ASP.NET 2.0 E-Commerce in C 2005
Pro ASP.NET 2.0 E-Commerce in C 2005 takes you through the e-commerce development cycle, from conception to coding to deployment and maintenance, world challenges, the book features a ease study of a fictional company. After d development are complete, the authors focus on utilizing the best deployment r maintaining (he application and allowing maximum scalability.
Foundations of WPF: An Introduction to Windows Presentation Foundation
Foundations of WPF: An Introduction to Windows Presentation Foundation b everything you need to get started with WPF technology. The book is broken int First introduces and contextualizes the WPF technology. The next part dives dee of the technology that are of immediate and valuable use in developing applicat offers you die real-world perspective you need to be productive in the communi base.UNQ for Visual C 2005 is a short yet comprehensive guide to the major fe thoroughly covers LINQ to Objects, UNQ to SQL, UNQ to DataSet, and UNQ to details significant enhancements to the next versions of C, .NET, and ADO.NET. includes plenty of working examples to demonstrate UNQ in action. There is no this book for getting a head start on the future of these technologies.
Local intranet | Protected Mode: Off
Figure 6-8. Recipe 6-5 final UI with data-bound Apress book information
Post a comment