473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

IsCallback with XMLHttpRequest object

How can I make a return from a standard XMLHttpRequest set the IsCallback on the server? Or how can I capture a result from a post in the OnLoad event, then return a string without having to go through the Rendor and PreRendor events? All I am trying to do is send a post to the server and return a result like the callback routines do without having to place the call on the callback stack.

Thanks,
Leo
Jun 27 '07 #1
3 2525
Frinavale
9,735 Expert Mod 8TB
How can I make a return from a standard XMLHttpRequest set the IsCallback on the server? Or how can I capture a result from a post in the OnLoad event, then return a string without having to go through the Rendor and PreRendor events? All I am trying to do is send a post to the server and return a result like the callback routines do without having to place the call on the callback stack.

Thanks,
Leo

I don't understand your question.
What are you trying to do?
Do you mean IsPostBack?
Usually you check the IsPostBack value in the PageLoad event.

I don't know what you mean by returning without going through the Render events....if you want to be able to view anything your application does in a web browser you have to go through these.

Please explain what it is you are trying to do (explain the problem you are trying to solve).

-Frinny
Jun 27 '07 #2
What I am trying to do is the following code

Expand|Select|Wrap|Line Numbers
  1. var xmlRequest
  2.  
  3. function CreateXMLHttpRequest()
  4. {
  5.     try
  6.     {   // Newer browsers such as Firefox, Opera and IE 7 use this object
  7.         xmlRequest = new XMLHttpRequest();
  8.     }
  9.     catch(err)
  10.     {   // This is what older versions of IE use (post IE 4)
  11.         xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
  12.     }
  13.  
  14. }
  15.  
  16. function MakeAjaxPost(strUrl, strVal)
  17. {
  18.     if(xmlRequest == null)
  19.         CreateXMLHttpRequest();
  20.  
  21.     if(xmlRequest != null)
  22.     {
  23.         xmlRequest.open('POST', strUrl, true);
  24.         xmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  25.         xmlRequest.onreadystatechange = function() 
  26.         {
  27.             if (xmlRequest.readyState == 4)
  28.             { 
  29.                 if(xmlRequest.status == 200 || xmlRequest.status == 304)
  30.                     ReturnRequestedByInfo(xmlRequest.responseText);
  31.                 else
  32.                     alert('Update Failed from \"MakeAjaxPost\".\n\n   Status: ' +
  33.                         xmlRequest.statusText + ' (' + xmlRequest.status + ')');
  34.             }
  35.         }
  36.         xmlRequest.send('__EVENTTARGET=&__EVENTARGUMENT=&' + strVal);
  37.     }
  38. }
  39.  
  40.  
The problem here is that when it get to the server I see a string I can use to post a page, when all I want to do is return a string similar to any normal callback function. The callback function goes from Page.Load to Page.RaiseCallbackEvent to Page.GetCallbackResult to Page.Unload and returns a result string to a new function, thus bypassing the PreRendor and Rendor functions with a page as a response file and not a simple string. I want to emulate the callback without having to use the normal callback functions, which have a couple limitations and bugs that have ran across.

The basic question is how can I post the data to the server and then return the desired results without having to postback to the server? IsCallback is the simplist way but the Page.IsCallback bit is read-only. If I can cancel everything at the Page.Load and still return a string, then I would be happy. I just don't want a webpage to return.

Thanks,
Leo
Jun 27 '07 #3
I came up with a work around (really nothing more than a hack) to make this work. It really isn't very elegant but it gets the job done. Here is the code.

Client Side JavaScript
Expand|Select|Wrap|Line Numbers
  1. //  Setup to use AJAX requests
  2. // This is function allows for asynchronous communication with server side
  3. // routines.  This allows for client side updates that will not require postbacks 
  4. // to the server.  
  5. var xmlRequest
  6.  
  7. function CreateXMLHttpRequest()
  8. {
  9.     try
  10.     {   // Newer browsers such as Firefox, Opera and IE 7 use this object
  11.         xmlRequest = new XMLHttpRequest();
  12.     }
  13.     catch(err)
  14.     {   // This is what older versions of IE use (post IE 4)
  15.         xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
  16.     }
  17.  
  18. }
  19.  
  20. //  Used to make the call using Post method
  21. //  strUrl - URL for page to launch
  22. //  strVal - Value to be sent to the server (string value only)
  23. //  retFunc - Name of function to return to from the server.
  24. //  retFunc - Name of function to return if there is an error
  25. //
  26. //  NOTE:  On functions, don't use the "()" when passing the names
  27. function MakeAjaxPost(strUrl, strVal, retFunc, retError)
  28. {
  29.     if(xmlRequest == null)
  30.         CreateXMLHttpRequest();
  31.  
  32.     if(xmlRequest != null)
  33.     {
  34.         xmlRequest.open('POST', strUrl, true);
  35.         xmlRequest.setRequestHeader('Content-Type', 
  36.                            'application/x-www-form-urlencoded');
  37.         xmlRequest.onreadystatechange = function() 
  38.         {
  39.             if (xmlRequest.readyState == 4)
  40.             { 
  41.                 if(xmlRequest.status == 200 || xmlRequest.status == 304)
  42.                     retFunc(xmlRequest.responseText);
  43.                 else
  44.                 {
  45.                     if(retError == null)
  46.                         alert('Update Failed from \"MakeAjaxPost\".\n\n   Status: ' +
  47.                                 xmlRequest.statusText + ' (' + xmlRequest.status + ')');
  48.                     else
  49.                         retError(xmlRequest.responseText);
  50.                 }
  51.             }
  52.         }
  53.         xmlRequest.send('__AJAXCALLBACK=&__AJAXSTRING=' + strVal);
  54.     }
  55. }
  56.  
  57.  
Server Side Code (in page class)
Expand|Select|Wrap|Line Numbers
  1.  
  2.     private string eventArgument = "";  // Used in GetCallbackResult() for 
  3.                                    // processing original request
  4.     private bool bIsXmlHttpRequest = false;
  5.  
  6.     protected void Page_Load(object sender, EventArgs e)
  7.     {
  8.         if ((string)Request["__AJAXCALLBACK"] != null)
  9.         {
  10.             string s = (string)Request["__AJAXSTRING"];
  11.             if (s != null)
  12.                 eventArgument = s;
  13.             else
  14.                 eventArgument = "";
  15.  
  16.             bIsXmlHttpRequest = true;
  17.         }
  18.         else
  19.             bIsXmlHttpRequest = false;
  20.     }
  21.  
  22.     protected override void OnPreRender(EventArgs e)
  23.     {
  24.         if (bIsXmlHttpRequest != true)
  25.         {
  26.             base.OnPreRender(e);
  27.         }
  28.     }
  29.  
  30.     protected override void Render(HtmlTextWriter writer)
  31.     {
  32.         if (bIsXmlHttpRequest == true)
  33.                        // Must return string for write to work
  34.             writer.Write(GetCallbackResult()); // Standard Callback Function but
  35.                        // can be any other function that returns a string and uses
  36.                        // the eventArgument variable
  37.         else
  38.             base.Render(writer);
  39.     }
  40.  
  41.  
Here is code I used to set this up and test this.
Expand|Select|Wrap|Line Numbers
  1. // Sever-Side 
  2. // Added in a setup function for page
  3. ctrl.Attributes.Add("onchange", "GetComboUpdate(this);");
  4.  
  5. // This function is needed if using ICallbackEventHandler and regular ASP.NET
  6. // Callbacks
  7. public string GetCallbackResult()
  8. {
  9.     int iFlag = 0;
  10.     string result = "succeeded<~>";
  11.  
  12.     string[] sTok = { "||" };
  13.     string[] sSplit = eventArgument.Split(sTok, 
  14.                              StringSplitOptions.RemoveEmptyEntries);
  15.     // Attempt to convert the string into an int for switch statement
  16.     if (int.TryParse(sSplit[0], out iFlag) == true)
  17.     {
  18.         if (IsUserValid() == true)      // Internal user validation
  19.         {
  20.             switch (iFlag)
  21.             {
  22.                 case 130:
  23.                     if(sSplit.Length == 3)
  24.                         result += eventArgument;  // Process function here for arg
  25.                     break;
  26.  
  27.                 default:
  28.                     result = "true";
  29.                     break;
  30.             }
  31.         }
  32.         else  // Not a valid user anymore so need to start validation process
  33.         {
  34.             result = "failed<~>" + eventArgument;
  35.         }
  36.     }
  37.     return result;
  38. }
  39.  
  40.  
  41.  
  42. // Client Script
  43. function GetComboUpdate(cb)
  44. {
  45.     // values to parse and pass for processing
  46.     var sVal = '130||' + cb.id + '||' + cb.options[cb.selectedIndex].value;
  47.     MakeAjaxPost('test.aspx', sVal, ReturnComboUpdate);
  48. }
  49. function ReturnComboUpdate(rtnVal)
  50. {
  51.     alert('Made it back:\n\n' + rtnVal);
  52. }
  53.  
Probably not very intuitive or even best practice. If there is something better I would be open to other solutions. You will note that context is missing, but it could probably be handled if necessary by extending the retFunc, and retError functions for the value.

Leo
Jun 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Matt Kruse | last post by:
I'm aware of the circular reference memory leak problem with IE/closures. I'm not sure exactly how to resolve it in this situation. Also, Firefox appears to grow its memory size with the same code....
5
by: Matt Kruse | last post by:
I'd like to test for Opera8.00's missing setRequestHeader method before actually instantiating the object. For example, this works in firefox: if (XMLHttpRequest.prototype.getRequestHeader) { ......
21
by: Joe Attardi | last post by:
Hey all! I was reading over at the IE Blog the other day http://http://blogs.msdn.com/ie/] and read some interesting, and encouraging news. According to Sunava Dutta, an IE Program Manager,...
1
by: weston | last post by:
So, the following apparently works as a method of grafting a method onto an object (using the squarefree JS shell): obj = { x: 1, inc: null } result obj.inc = function () { this.x++ } result...
13
by: TLaufenberg | last post by:
I'm new to Javascript programming and I've run into a bit of a snag with making an XMLHttpRequest in the Safari browser. Actually, the request doesn't work in Firefox either but only when I use a...
5
by: Peter Michaux | last post by:
Hi, The FAQ correctly says the following: "Mozilla (NN6.2+, Firefox, Ice Weasle etc), Opera 7.6+, Safari1.2+, the Windows version of IE versions 5+, and some other browsers provide the XML...
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
6
by: Patrick Nolan | last post by:
I'm working on cross-platform portability of some javascript. My Macintosh testing platform is rather old. It has Safari 1.3.2 and Internet Explorer 5.2. I got Safari working, but now IE is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.