IE9 refresh after Ajax request returns - asp.net

I have a JavaScript class that displays a partially-opaque div over top of the content of another div when an Ajax request is sent to the server.
When the request returns, the JavaScript class hides the partially-opaque div....it works great...sort of.
Right now, in IE9, when the Ajax request is complete, the partial-opacity is only hidden if the user moves their mouse.
So, my question is, how do I force the browser to do what it's supposed to do?
This is my extremely simple function that is called after the request returns to the browser:
_hideBlockingDiv: function() {
if (this.get_blockingDivClientID()) {
var blockingElement = $get(this.get_blockingDivClientID());
if (blockingElement != null) {
blockingElement.style.display = 'none';
//I know that this method is executing correctly because I "hi" showed
//up properly...but the element remained visible:
blockingElement.innerHTML = 'hi';
}
//if I add the alert then everything works fine in IE9
//if I don't then the page will remain the same until the user moves their mose
//alert("done");
}
}
Please note that I am not using JQuery.
I am using the AJAX.NET library since I am a .NET developer (and JQuery didn't become popular until years after I implemented my Ajax-enabled server controls)
Thanks
-Frinny

How and where do you call the _hideBlockingDiv function from? Since you are using MS Ajax library, you might want to have a page loaded handler on client side and call this function from within that handler. So basically
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function(){
_hideBlockingDiv();
});
Hope this helps!

It turns out that the problem only exists in the beta version of IE9 that I was using at the time. This problem went away once the full version of IE9 was released.

Related

Progress cursor not seen on iPad

I'm coding a website and trying to work out best practise for displaying a progress cursor. As my site is using an Ajax request to do the main heavy lifting, I've simply selected the "progress cursor" in CSS and apply it via jQuery at the start of my Ajax request and set it back to cursor:auto by applying another class with jQuery when I get the response back. Now this works well on my PC and laptops, but does not seem to work on my iPad. Everything works fine but the cursor on the iPad does not change when the Ajax request fires. Has anyone had a similar 'problem' or am I missing something that needs to be done on iPad / Mac, or perhaps not doing this right?!
My CSS code to set cursor :
body.wait { cursor: progress !important; }
My jQuery to apply cursor at start of Ajax request
$('html, body, button').css("cursor", "wait");
..and to set it back on completion of request:
$('html, body, button').css("cursor", "auto");
Many thanks for any ideas of how to get it working on iPad!

Submit form via AJAX with loading progress?

just need tips on how to make forms where request are submitted via AJAX with a loading progress image. I am using update panels with AJAX framework. I would like to know about the recommended approach. Through JQuery or AJAX toolkit ?
Please advice, examples would be an added bonus for me.
1- Prepare a client side div with "display:none" style property. put your loading image inside.
2 - when the user or page submits a request, change that divs display property to "block".
3- Add some kind of "information received" sign to the response and check this response from the client side and then change that divs display property back to "none"
I would like to know about the
recommended approach
Well, that depends on what you are doing, what parts of the form are you updating, how big is the form, what values are you sending to the server.
Generally speaking, if you want to update something simple (dropdownlist, listbox, etc), youd generally use JavaScript (or jQuery) to call an AJAX-enabled web service. This way, you're only sending to the server the data it needs, things like ViewState/cookies are not sent over the wire. You also have full control over the pre/post execution events (so you can add your loading images, call the WS, then clear them).
However, if you want to asynchronously update an entire form (which has a lot of controls), you're probably right in using an UpdatePanel. Things like a GridView are a good case for an UpdatePanel (as you usually need to handle editing, binding and paging all asynchronously).
The progress image is made easy with the following code:
<ProgressTemplate>
<img src="someloadingimage.gif" alt="Loading" />
</ProgressTemplate>
Stick that inside your UpdatePanel, and whenever an AJAX call is made, the loading image will be shown.
HTH
If you use JQuery for AJAX request then you can use the following events -
$.ajax({ url: "test.html",
type: "GET",
beforeSend: function(){
-----load your loader here-----
});,
success: function(){
------remove your loader here -----------
Remaining code
}});
You can also use POST. in above example i have used GET.
For detailed documentation you can refer - http://api.jquery.com/jQuery.ajax/
Create a small plug-in for your loader like so.
$.fn.ShowLoader = function(on){
switch(on)
{
case true:
$(this).show();
break;
default:
$(this).hide();
break;
}
}
then use the following:
$('form').submit(function(){
var Form = $(this);
$('.loader',Form).ShowLoader(true);
//Gather some params
Location = Form.attr('src');
Data = Form.Serialize();
$.post(Location,Data,function(result){
result = result || false;
if(result)
{
$('.loader',Form).ShowLoader(false); //Disable the loader
//Process result
}
});
})
html would just be a regular form, with an image / div inside with the class of loader

ajaxSubmit and Other Code. Can someone help me determine what this code is doing?

I've inherited some code that I need to debug. It isn't working at present. My task is to get it to work. No other requirements have been given to me. No, this isn't homework, this is a maintenance nightmare job.
ASP.Net (Framework 3.5), C#, jQuery 1.4.2. This project makes heavy use of jQuery and AJAX. There is a drop down on a page that, when an item is chosen, is supposed to add that item (it's a user) to an object in the database.
To accomplish this, the previous programmer first, on page load, dynamically loads the entire page through AJAX. To do this, he's got 5 div's, and each one is loaded from a jQuery call to a different full page in the website.
Somehow, the HTML and BODY and all the other stuff is stripped out and the contents of the div are loaded with the content of the aspx page. Which seems incredibly wrong to me since it relies on the browser to magically strip out html, head, body, form tags and merge with the existing html head body form tags.
Also, as the "content" page is returned as a string, the previous programmer has this code running on it before it is appended to the div:
function CleanupResponseText(responseText, uniqueName) {
responseText = responseText.replace("theForm.submit();", "SubmitSubForm(theForm, $(theForm).parent());");
responseText = responseText.replace(new RegExp("theForm", "g"), uniqueName);
responseText = responseText.replace(new RegExp("doPostBack", "g"), "doPostBack" + uniqueName);
return responseText;
}
When the dropdown itself fires it's onchange event, here is the code that gets fired:
function SubmitSubForm(form, container) {
//ShowLoading(container);
$(form).ajaxSubmit( {
url: $(form).attr("action"),
success: function(responseText) {
$(container).html(CleanupResponseText(responseText, form.id));
$("form", container).css("margin-top", "0").css("padding-top", "0");
//HideLoading(container);
}
}
);
}
This blows up in IE, with the message that "Microsoft JScript runtime error: Object doesn't support this property or method" -- which, I think, has to be that $(form).ajaxSubmit method doesn't exist.
What is this code really trying to do? I am so turned around right now that I think my only option is to scrap everything and start over. But I'd rather not do that unless necessary.
Is this code good? Is it working against .Net, and is that why we are having issues?
A google search for
jquery ajax submit
reveals the jQuery Form Plugin. Given that, is that file included on your page where the other code will have access to the method? Does this code work in Firefox and not IE?
Seems like there was too much jQuery fun going on. I completely reworked the entire code block since it was poorly designed in the first place.

How do I POST to a web page using Firebug?

How do I POST to a web page using Firebug?
You can send POST request to any page by opening console (e.g. in FireFox ctrl + shift + k) and typing simple JS:
var formPost = document.createElement('form');
formPost.method = 'POST';
formPost.action = 'https://www.google.com'; //or any location you want
document.body.appendChild(formPost);
formPost.submit();
AFAIK Firebug can't do this. However, there is a very useful Firefox extension, in the spirit of Firebug, called Tamper Data. This should be able to do what you want.
It allows you to monitor each request made by the browser, and you can turn on an option that allows you to look at, and edit, every single request before it gets sent.
Firefox 27 (maybe earlier versions too, never checked) has built-in developer tools to modify and resend requests. If you don't have Firebug installed, the console is available by pressing the F12 key. If Firebug is installed, press Ctrl+Shift+K instead.
I know this is an old question, but I recently stumbled upon the same problem and wanted to share the method I am using.
Assuming the web site you want to POST to has a form with method="POST" (a very likely scenario), you can use Firebug's JavaScript command line to programmatically submit a POST request. Just click the "Show Command Line" icon in Firebug and enter something like this in the narrow text box at the very bottom of the window:
document.forms[0].submit()
Maybe this helps someone.
Another simple solution is to load any webpage that uses jQuery, and type up a $.post() in the console.
HTTP resource test is a firefox plugin that can do this.
Another powerful Firefox plugin to perform post request and some more features is the Hackbar.
Related:
To resend a POST already made, right click the POST request in the Net/XHR view and click "Resend".
Using Firebug 1.12.0:
Got here looking for a Firebug way of doing this. Then I realized that I could use Fiddler. This is the most powerful tool I know when it comes to debugging web requests.
Fiddler The free web debugging proxy for any browser, system or
platform
Click the Composer tab and write your request as desired - then click Execute.
NO NEED of plugins !!
Just drag any url in BOOKMARK BAR, then right click and EDIT, and insert javascript code:
javascript:var my_params=prompt("Enter your parameters","var1=aaaa&var2=bbbbb"); var Target_LINK=prompt("Enter destination", location.href); function post(path, params) { var form = document.createElement("form"); form.setAttribute("method", "post"); form.setAttribute("action", path); for(var key in params) { if(params.hasOwnProperty(key)) { var hiddenField = document.createElement("input"); hiddenField.setAttribute("name", key); hiddenField.setAttribute("value", params[key]); form.appendChild(hiddenField); } } document.body.appendChild(form); form.submit(); } parsed_params={}; my_params.substr(1).split("&").forEach(function(item) {var s = item.split("="), k=s[0], v=s[1]; parsed_params[k] = v;}); post(Target_LINK, parsed_params); void(0);
then enter the target site-link, and click that button in BOOKMARK BAR! That's all!
( source: https://stackoverflow.com/a/38643171/2377343 )

Displaying Loading text while doing a WebRequest

I have a button on my webform. Clicking this button will do an HttpWebRequest during the onclick event handler. After the request we copy the response from the request into HttpContext.Current.Response and send that to the client.
This web request can take a while (up to 5 seconds, since it's generating a report). During this time the user has no indication that anything is going on, except for the browser progress bar and the spinning IE icon (if they're using IE). So I need a loading indicator while this is happening.
I've tried using javascript that fires during the button's onclick event (using OnClientClick) and while that works, I don't know how to find out when the web request is finished. Since we just send the response to the client, a full postback doesn't happen.
I've tried wrapping the button in an UpdatePanel and using the UpdateProgress, but when we send the response to HttpContext.Current.Response and call Response.End(), we get an error in the javascript, since the response isn't well formed (we're sending back an excel sheet for the user to download).
Since we're sending back a file for users to download, I don't want to pop-up a separate window, since then in IE they'd get the information bar blocking the download.
Any ideas here?
As an alternative to the Professional AJAX.NET library, jQuery has a really nice way of doing this.
Take a look at this example of using a .NET PageMethod (if possible in your scenario).
You define a page method call in jQuery, you can tack on your loading... message in a hidden div.
Say what callback you want to return on success (ie when your 5 second report is generated)
then hide the loading and handle the data.
Take a look at the javascript on my contact page for an example (view the source).
I have a a button on the page, add the jQuery onClick.
When clicked that shows a hidden loading div, makes an ajax call to a page method that takes the parameters of the form.
The page method does emailing etc then returns to the form in the onSuccess javascript method I have there.
The onSuccess hides the loading div.
A simple trick i have used in the past is to redirect to an intermediate page with an animated progress bar (gif) and then have that page do the REAL post of the data.
(or even pop-up a layer with the animation on it and a polite message asking the user to wait a minute or two)
The simple feedback of the animated gif creates the illusion to the end user that the app is not stalled and they will be more patient.
Another approach is to hand the data off to a worker thread and return immediately with a message stating that the report will be emailed or made available in the "reports" section of the site when it is ready. This approach lacks the benefit of instant notification when the report is completed though.
Here is my solution :
Download and examine the samples of free Professional AJAX.NET library.
Write a AjaxMethod that creates your file and returns file location as a parameter.
Write your Client-Side function to call method at Step 2. When this method called show an indicator.
Write a client-side callback method to hide indicator and show/download file that user requested.
Add your client-side function calls yo your button element.
When your method at server-side ends your callback will be called.
Hope this helps !
The solution I'm presenting here is aimed to show a method to let a "Loading..." box to appear while you're server-side processing and to disappear when server-side processing is complete.
I'll do this with the very basic AJAX machinery (tested on FF, but IE should be ok either), i.e. not using a framework like Prototype or jQuery or Dojo, as you didn't specify your knowledge about them.
To let you better understand the trick, the following is just a small example and doesn't pretend to be an out-of-the-box solution. I tend not to be superficial, but I think a clearer example can explain better than many words.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>First Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
.hidden {
display: none;
}
.loadingInProgress {
color: #FFFFFF;
width: 75px;
background-color: #FF0000;
}
</style>
<script type="text/javascript">
var httpRequest;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
httpRequest.overrideMimeType('text/xml');
} else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
}
httpRequest.onreadystatechange = function(){
switch (httpRequest.readyState) {
case 1: // Loading
document.getElementById('loading').className = "loadingInProgress";
break;
case 4: // Complete
document.getElementById('loading').className = "hidden";
if (httpRequest.status == 200) {
// perfect!
} else {
// there was a problem with the request,
// for example the response may be a 404 (Not Found)
// or 500 (Internal Server Error) response codes
}
break;
}
};
function go() {
httpRequest.open('GET', document.getElementById('form1').action, true);
httpRequest.send('');
}
</script>
</head>
<body>
<div id="loading" class="hidden">Loading...</div>
<form id="form1" name="form1" action="doSomething.php">
<input type="button" value="Click to submit:" onclick="go()" />
</form>
</body>
</html>
As you can see, there's a <div> which holds the "Loading..." message.
The principle is to show/hide the <div> depending on the XMLHttpRequest object's readyState.
I've used the onreadystatechange handler of the XMLHttpRequest to trigger the readyState change.
The back-end php script I use (declared as the form's action) does just a sleep(5), to let the "Loading..." message appear for 5 secs.
<?php
sleep(5);
header('Cache-Control: no-cache');
echo "OK";
?>
The Cache-control: no-cache header is necessary, since usually if you don't set it the browser will cache the response avoiding to resubmit the request if you should need to.
A good source for "getting started" AJAX documentation is Mozilla MDC.
The whole thing could be much more gently handled by a Javascript framework like Prototype, taking advantage of its browser-safe approach, saving you hours of debug.
Edit:
I chose php 'cause I don't know ASP.NET nor ASP, sorry about that.

Resources