10.02.2014 Views

Beginning Ajax With ASP.NET (2006).pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Next listed in the following code blocks are the JavaScript functions that accompany the page listing.<br />

The first function is a generic function to simply create an XMLHttpRequest object that you can use:<br />

<br />

<br />

<br />

Untitled Page<br />

<br />

The XMLHttpRequest Object<br />

// A “global” variable that is our XMLHttpRequest object reference.<br />

var xmlHttpObj = CreateXmlHttpRequestObject();<br />

The next function is what is called when the document first loads and deals with loading the customer<br />

data from the server using the XMLHttpRequest object:<br />

// Function to load the customer selection data into the drop list control<br />

function LoadCustomers()<br />

{<br />

if (xmlHttpObj)<br />

{<br />

// We want this request synchronous<br />

xmlHttpObj.open(“GET”,”http:// “ + location.host +<br />

“/XmlHttp_Chap4/DataFile.xml”, false);<br />

// Execute the request<br />

xmlHttpObj.send(null);<br />

// If the request was ok (ie. equal to a Http Status code of 200)<br />

if (xmlHttpObj.status == 200)<br />

{<br />

var xmlDoc = xmlHttpObj.responseXML;<br />

// Our list of nodes selected using the X Path argument<br />

//var nodes = xmlDoc.selectNodes(“//Customers/Customer”);<br />

var nodes = xmlDoc.selectNodes(“//Customers/Customer/Lastname/text()”);<br />

// Obtain a reference to the drop list control.<br />

var ctrl = document.getElementById(“ddlCustomers”);<br />

for (var i=0; i < nodes.length; i++)<br />

{<br />

// Get the lastname element from our XML data document<br />

var lastName = nodes[i].nodeValue;<br />

// Create a new node.<br />

var htmlCode = document.createElement(‘option’);<br />

// Add the new node to our drop list<br />

ctrl.options.add(htmlCode);<br />

// Set the display text and value;<br />

htmlCode.text = lastName;<br />

htmlCode.value = lastName;<br />

}<br />

} else<br />

89

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!