Can you tell me , why this code does not run:
<%@ Page Language="C#"
MasterPageFile="~/MasterPage.master"
AutoEventWireup="true"
CodeFile="AJAX6.aspx.cs"
Inherits="Experiments_AJAX_AJAX6"
Title="XML and SQL" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent"
Runat="Server">
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function doSearch() {
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "/App_Data/books.xml", true);
xmlHttp.send(null);
}
function startRequest(requestedList) {
requestType = requestedList;
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
//alert(xmlHttp.onreadystatechange);
xmlHttp.open("GET", "/App_Data/books.xml", true);
xmlHttp.send(null);
}
function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
clearPreviousResults();
if( requestType == "author" )
booksByAuthor();
else
listAllBooks();
}
}
}
function clearPreviousResults() {
var header = document.getElementById("header");
if(header.hasChildNodes()) {
header.removeChild(header.childNodes[0]);
}
var tableBody = document.getElementById("resultsBody");
while(tableBody.childNodes.length 0) {
tableBody.removeChild(tableBody.childNodes[0]);
}
}
function listAllBooks() {
var results = xmlHttp.responseXML;
var book = null;
var isbn = "";
var year = "";
var title = "";
var author = "";
var book_author = "";
var keyword = "";
var book_keyword = "abc";
addTableRow("ISBN", "TITLE" , "AUTHOR","YEAR", "KEYWORD");
var book = results.getElementsByTagName("book");
for(var i = 0; i < book.length; i++) {
isbn = book[i].getElementsByTagName("isbn")
[0].firstChild.nodeValue;
year = book[i].getElementsByTagName("year")
[0].firstChild.nodeValue;
title = book[i].getElementsByTagName("title")
[0].firstChild.nodeValue;
keyword = "";
book_keyword = book[i].getElementsByTagName("b_keywords");
for ( var j = 0; j < book_keyword[0].childNodes.length ; j++ )
{
if (keyword)
keyword = keyword+ ", " +
book_keyword[0].getElementsByTagName("keyword")
[j].firstChild.nodeValue;
else
keyword = book_keyword[0].getElementsByTagName("keyword")
[j].firstChild.nodeValue;
}
author = "";
book_author = book[i].getElementsByTagName("b_authors");
for ( var j = 0; j < book_author[0].childNodes.length ; j++ )
{
if ( author )
author = author+ ", " +
book_author[0].getElementsByTagName("author")[j].firstChild.nodeValue;
else
author = book_author[0].getElementsByTagName("author")
[j].firstChild.nodeValue;
}
addTableRow(isbn, title, author, year,keyword);
}
var header = document.createElement("h3");
var headerText = document.createTextNode("Results:");
header.appendChild(headerText);
document.getElementById("header").appendChild(head er);
document.getElementById("resultsTable").setAttribu te("border",
"1");
}
function booksByAuthor() {
var results = xmlHttp.responseXML;
var book = null;
var isbn = "";
var year = "";
var title = "";
var author = "";
var book_author = "";
var keyword = "";
var book_keyword = "abc";
var authorName = "";
addTableRow("ISBN", "TITLE" , "AUTHOR","YEAR", "KEYWORD");
var book = results.getElementsByTagName("book");
for(var i = 0; i < book.length; i++) {
isbn = book[i].getElementsByTagName("isbn")
[0].firstChild.nodeValue;
year = book[i].getElementsByTagName("year")
[0].firstChild.nodeValue;
title = book[i].getElementsByTagName("title")
[0].firstChild.nodeValue;
keyword = "";
book_keyword = book[i].getElementsByTagName("b_keywords");
for ( var j = 0; j < book_keyword[0].childNodes.length ; j++ )
{
if (keyword)
keyword = keyword+ ", " +
book_keyword[0].getElementsByTagName("keyword")
[j].firstChild.nodeValue;
else
keyword = book_keyword[0].getElementsByTagName("keyword")
[j].firstChild.nodeValue;
}
author = "";
book_author = book[i].getElementsByTagName("b_authors");
for ( var j = 0; j < book_author[0].childNodes.length ; j++ )
{
if ( author )
author = author+ ", " +
book_author[0].getElementsByTagName("author")[j].firstChild.nodeValue;
else
author = book_author[0].getElementsByTagName("author")
[j].firstChild.nodeValue;
}
authorName = document.getElementById("authorName") ;
for ( j = 0; j < authorName.options.length ; j++ )
if ( authorName.options[j].selected )
if( author.indexOf( authorName.options[j].value ) !=
-1 )
addTableRow(isbn, title, author, year,keyword);
}
var header = document.createElement("h3");
var headerText = document.createTextNode("Results:");
header.appendChild(headerText);
document.getElementById("header").appendChild(head er);
document.getElementById("resultsTable").setAttribu te("border",
"1");
}
function addTableRow(isbn, title, authors, year,keywords) {
var row = document.createElement("tr");
var cell = createCellWithText(isbn);
row.appendChild(cell);
cell = createCellWithText(title);
row.appendChild(cell);
cell = createCellWithText(authors);
row.appendChild(cell);
cell = createCellWithText(year);
row.appendChild(cell);
cell = createCellWithText(keywords);
row.appendChild(cell);
document.getElementById("resultsBody").appendChild (row);
}
function addTableHeading(isbn, title, authors, year,keywords) {
var thElement = document.createElement("THEAD");
var trElement = document.createElement("tr");
thElement.appendChild(trElement);
var row = document.createElement("th");
trElement.appendChild(row);
var cell = createCellWithText(isbn);
row.appendChild(cell);
var row = document.createElement("th");
trElement.appendChild(row);
cell = createCellWithText(title);
row.appendChild(cell);
var row = document.createElement("th");
trElement.appendChild(row);
cell = createCellWithText(authors);
row.appendChild(cell);
var row = document.createElement("th");
trElement.appendChild(row);
cell = createCellWithText(year);
row.appendChild(cell);
var row = document.createElement("th");
trElement.appendChild(row);
cell = createCellWithText(keywords);
row.appendChild(cell);
document.getElementById("resultsBody").appendChild (row);
}
function createCellWithText(text) {
var cell = document.createElement("td");
var textNode = document.createTextNode(text);
cell.appendChild(textNode);
return cell;
}
</script>
<center>
<h4 align = "center"><font color = "#000" size = "5">Search
Books !!</font</h4>
<br/>
<table>
<tr>
<td><input type="button" value="All Books"
onclick="startRequest('all')"/></td>
</tr>
<tr>
<td> List books by:
</td>
</tr>
<tr><td>
<select id="authorName">
<option value="" >Select Author</option>
<option value="Harvey M. Deitel">Harvey M. Deitel</option>
<option value="Stephen King">Stephen King</option>
<option value="Paul J. Deitel">Paul J. Deitel</option>
<option value="Larry Wall">Larry Wall</option>
<option value="Jon Orwant">Jon Orwant</option>
<option value="Adam W. Chase">Adam W. Chase</option>
<option value="Ed Kugler">Ed Kugler</option>
<option value="Eric Harr">Eric Harr</option>
<option value="Charles W. Henderson">Charles W.
Henderson</option>
</select>
<input type="button" value="Search"
onclick="startRequest('author')"/></td>
</tr>
</table>
<span id="header">
</span>
<table id="resultsTable" width="75%" >
<tbody id="resultsBody">
</tbody>
</table>
</center>
</asp:Content>
The XML file is:
<?xml version="1.0" encoding="ISO-8859-1"?>
<myBooks>
<book>
<isbn>0743289412</isbn>
<title>Lisey's Story</title>
<b_authors>
<author>Stephen King</author>
</b_authors>
<year>2006</year>
<b_keywords>
<keyword>Novel</keyword>
</b_keywords>
</book>
<book>
<isbn>0451211243</isbn>
<title>The Dark Tower Box Set</title>
<b_authors>
<author>Stephen King</author>
</b_authors>
<year>2003</year>
<b_keywords>
<keyword>Novel</keyword>
</b_keywords>
</book>
<book>
<isbn>0131483986</isbn>
<title>Java: How to Program</title>
<b_authors>
<author>Harvey M. Deitel</author>
<author>Paul J. Deitel</author>
</b_authors>
<year>2004</year>
<b_keywords>
<keyword>Java</keyword>
</b_keywords>
</book>
<book>
<isbn>0596000278</isbn>
<title>Programming Perl</title>
<b_authors>
<author>Larry Wall</author>
<author>Jon Orwant</author>
</b_authors>
<year>2000</year>
<b_keywords>
<keyword>Perl</keyword>
</b_keywords>
</book>
<book>
<isbn>1579547486</isbn>
<title>Triathlon Training for the Rest of Us</title>
<b_authors>
<author>Eric Harr</author>
</b_authors>
<year>2003</year>
<b_keywords>
<keyword>Triathlon</keyword>
</b_keywords>
</book>
<book>
<isbn>0425103552</isbn>
<title>Marine Sniper</title>
<b_authors>
<author>Charles W. Henderson</author>
</b_authors>
<year>1988</year>
<b_keywords>
<keyword>sniper</keyword>
</b_keywords>
</book>
</myBooks>