I have an HTML form that calls an ASP.NET page when a button is pressed using JavaScript’s window.location. The ASP.NET page runs a process and prompts the user to save the results of the process to a text file on the user’s computer.
Because the ouput of the ASP.NET page is a text file, the user never actually navigates away from the HTML form–that is the behavior I want. However, the process involved in generating the text file can take a minute or so. Hence, I’d like my HTML form to display a “Loading” message while the page is running and remove the message when the page is done running.
How can my HTML form know the state of the page it calls? In other words, how can I know when to remove the “Loading” message from the HTML form?
I ended up using a callback with an ASPX WebMethod to solve the issue. Since an ASPX WebMethod cannot save a text file to the browser, I write the file to disk and then return the URL of the file, prompting the user to save it.
At a high level, you need to make an AJAX call from your JavaScript to some kind of endpoint on the server. Sounds like you’re partway there already, but instead of calling the page itself (I’m guessing you’re doing a simulated postback from JavaScript), you’d need to call a something that knows how to return a value to your calling JavaScript. An ASP.NET
WebMethodis one approach; you could also create a web service. TheWebMethodapproach would be closer to what you’re doing now.The
WebMethodwould then return a JSON or XML message containing a success/failure message.In your JavaScript, then, the flow goes like this:
WebMethodon the server, along with a callback function that will be invoked when the server returns a resultWebMethodprocesses the formIf you include more details about the technologies you’re using, I could be more specific.