Jump to content

Returning A Value From A Function Using Xmlhttprequest


phiphy
 Share

Recommended Posts

Hi, I hope someone could help me with this problem.Here is the code:<asp:Button ID="btnPokreni" runat="server" Text="Ponovi slanje" Width="300px" onclick="btnPokreni_Click" OnClientClick = "return GetBatchProcessStatus('start');" UseSubmitBehavior="True"/>function GetBatchProcessStatus(tip) { var url = "BatchProcessStatus.asmx/BatchInProgress?type=" + tip; xmlHttp.open("GET", url, true); // true indicates asynchronous request xmlHttp.onreadystatechange = checkStatusStart; xmlHttp.send(null); return ???;}function checkStatusStart() { if (xmlHttp.readyState == 4) // Completed operation { var status = xmlHttp.responseXML.text; var retVal = poruka(status); return retVal ; }}I need to return the boolean value that function poruka(status) returns. The problem is that i get to return ??? in GetBatchProcessStatus before poruka(status) gets executed. Depending on this value (true/false), the code behind should or should not execute.Any ideas how to accomplish this?

Link to comment
Share on other sites

Ajax requests are asynchronous, so the checkStatusStart function is going to run later than the GetBatchProcessStatus function, GetBatchProcessStatus will always finish before checkStatusStart. You can't have the checkStatusStart function return a value to GetBatchProcessStatus because GetBatchProcessStatus does not call checkStatusStart, checkStatusStart gets called automatically as an event handler. You'll need to find a different way to do what you're trying to do, if you want to explain what you're trying to accomplish we might be able to give suggestions.

Link to comment
Share on other sites

Thank you for your quick responses. Basically, I have to interrogate some DB data first (that's why I use XMLHttpRequest) and depending of what I found there I need to execute or not the code behind.Rnd me's idea sounds like it could solve my problem. I could check the return value of poruka(status) and if true, I would execute the code behind. Only, how can I do this? How do I call btnPokreni_Click from JS?Thank you!

Link to comment
Share on other sites

Ok, I did this...[i]<asp:Button ID="btnPokreni" runat="server" Text="Ponovi slanje" Width="300px" [b]OnClientClick = "firstCheck();[/b]" UseSubmitBehavior="false"/>[/i]protected void Page_Load(object sender, EventArgs e) { // Insure that the __doPostBack() JavaScript method is created... ClientScript.GetPostBackEventReference(this, string.Empty); if (this.IsPostBack) { string eventTarget = (this.Request["__EVENTTARGET"] == null) ? string.Empty : this.Request["__EVENTTARGET"]; string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"]; if (eventTarget == "btnPokreni_Click") { KonzolaBatch1.MyOnButtonClick(); } } }[i]function GetBatchProcessStatus(tip) { debugger; var url = "BatchProcessStatus.asmx/BatchInProgress?type=" + tip; xmlHttp.open("GET", url, true); // true indicates asynchronous request xmlHttp.onreadystatechange = checkStatusStart; xmlHttp.send(null); return false;}[/i]function checkStatusStart() { if (xmlHttp.readyState == 4) // Completed operation { var status = xmlHttp.responseXML.text; if (status == '' || status == null) { [b]__doPostBack('btnPokreni_Click', '');[/b] } else { $get("ctl00_ContentPlaceHolder1_KonzolaBatch1_txtProgress").value = status; } } return false;}[i]function [b]firstCheck()[/b] { ...some code... if (count > 0) { [b]GetBatchProcessStatus('start');[/b] return false; } return false;}[/i]Is it possible to do a postback without reloading the whole page? Let me explain better...since I use update panels, on (let's call them) normal postbacks I don't see the whole page changing. With the forced postback (__doPostBack) the page reloads (I get Waiting for http://... in status bar). Is it possible to do a partial postback from JS? Edited by phiphy
Link to comment
Share on other sites

I'm not sure what you mean by a partial postback, you either send a post request or not. You can either use Javascript to send the request using ajax, which won't refresh the page, or you can submit a form on the page to send the request, which will cause the page to refresh. Any logic you need for handling the data that comes back from the post request needs to go in the response handler for the ajax call, so all of that logic needs to go in the checkStatusStart function. That's the only place you can run any code that depends on the data coming back from the server.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...