Real Time Data Grid Part 2/2 - The JavaScript code (contd.)
(Page 3 of 6 )
Our SendRemoteRequest JavaScript function creates a new instance of the MSXML2.XMLHTTP class, which allows us to retrieve the contents of any page on a HTTP web server (assuming that we have the permissions to do so). The strPage variable contains the URL to the page we want to retrieve. You'll notice that the strPage variable contains the URL of our dynamic.datagrid.asp page, as well as the variable of the query argument tacked onto the end.
Next, we call the open method of our XMLHTTP object, and actually retrieve the requested page using its send method. We wont go into too much detail about the XMLHTTP library in this article, but if you'd like to read up on it, then checkout Mitchell's article on it
here. The page being retrieved is our ASP script, but with a query string variable named "query" passed to it.
At the top of our ASP script, before the declaration of our DynamicGrid class, we have the following lines:
strQuery = Request.QueryString("query")
If strQuery <> "" Then
RunQuery(Server.HTMLEncode(strQuery))
Response.End
End IfWhen we request this page using the XMLHTTP object through JavaScript, it sees that we have passed in a query string variable named query and calls the RunQuery method. The RunQuery method executes this query against our database and returns either true/false, indicating whether or not the query succeeded:
objConn.Open strConnectionstring
objComm.ActiveConnection = objConn
objComm.CommandText = Query
objComm.Execute
If err.number = 0 And objConn.Errors.Count = 0 Then
Response.Write "true"
Else
Response.Write "false"
End IfAs soon as the RunQuery sub is finished, the Response.End method tells our XMLHTTP object that it has finished. We capture the output from this page using the responseText member of our XMLHTTP object and return it from our JavaScript function:
return objXMLHTTP.responseText;Because our RunQuery ASP routine suppressed all error messages, the responseText method will only return the string "true" or "false". Jumping back to our doUpdate JavaScript function, we check the value returned from the call to the SendRemoteRequest function:
blnSuccess = SendRemoteRequest(strQuery);
if(blnSuccess == "true")
{
window.status = "Field '"+strFieldName+"' updated for record #"+(recordId+1)+" successfully.";
setTimeout("window.status=''", 2000);
}
else
{
alert("An error occured while trying to update record.");
}If it's "true", then the SendRemoteRequest function succeeded and updated our database successfully. We set the status text of the browser window to let the user know that record was updated, and use the setTimeout function to set the status text back to nothing after two seconds:

If the SendRemoteRequest function failed, then we display a message box telling the user that the record couldn't be updated:

Next: The doDel JavaScript function >>
More ASP Articles
More By Annette Tennison