Ajax Faqs

Ajax Faqs

---Is there any way that an AJAX object can get back a record set?

Answer

You could build an XML document out of your recordset and send that back to the server, say you had a redord set for a "user" with the following details (name, surname, age, email), you could build an xml document like this:

Code:
<recordset>
     <user>
         <name>Byron</name>
         <surname>Tymvios</surname>
         <age>25</age>
         <email>[email protected]</email>
     </user>
     <user>
         <name>User</name>
         <surname>Someone</surname>
         <age>39</age>
         <email>[email protected]</email>
     </user>
</recordset>

You can add as many records as you have in your recordset, then once the client has received it you can use javascript to iterate over the <user>'s in the xml.


---Is it possible to set session variables from javascript?
It's not possible to set any session variables directly from javascript as it is purely a client side technology. You can use AJAX though to asyncronously send a request to a servlet running on the server and then add the data to the session from within the servlet. So you wouldn't be using javascript to do the actual setting of session variables but it would look like it is.
  I've been developing a sliding navigational menu. Here is how I save the state across pages:

var saveState = true;
if(saveState)
{
// This AJAX call will save the Navigator's state to session.
// We don't need a callback function because nothing happens
// once said state is saved.
var url = "AJAX_Servlet.aspx?function=saveNavigatorState&control=" + id + "&class=" + section.className + "";
req = new ActiveXObject("Microsoft.XMLHTTP");
req.open("POST", url, true);
req.send();
}


Note that id is set to the Id of the submenu table (which is hidden / shown by other code which sets the className of the table. My stylesheet has css for each class.) earlier in the overal function.
Here is the codebehind for my AJAX_Servlet.aspx, which could easily be a web service:

private void Page_Load(object sender, System.EventArgs e)
{
if(Request.QueryString["function"] != null)
{
if(Request.QueryString["function"] == "saveNavigatorState")
SaveNavigatorState();
}
}
private void SaveNavigatorState()
{
if(Request.QueryString["control"] != null && Request.QueryString["class"] != null)
{
string controlID = Request.QueryString["control"].ToString();
string className = Request.QueryString["class"].ToString();
Session[controlID] = className;
}
}


Then on my navigator codebehind, I just check for session data on load.

你可能感兴趣的:(Ajax Faqs)