I have been trying to Implement Loading Gif without using the update panel in ASP.Net.
While researching on how to do this. I came across multiple ways to do this.
1: Using JQueryÂ
use the ajaxStart and ajaxStop function
If you put these functions to a div on a page like shown below the div will show automatically when an Ajax event starts and will hide when the event ends. loading is the div’s id in the below function.
 $("#loading").ajaxStart(function(){
 $(this).show();  }).ajaxStop(function(){
  $(this).hide(); Â
});
You can use the same method shown above to implement css classes for the div showing the loading gif and an opaque div on top of the elements on the form so that the user does not click anything until the event has executed.
2: Using JQuery with your own code while there is no Ajax or Partial Postback
ASPX code:
<div id="divSiteFrame" style="background-color: #777777; display: none; left: 0px;
position: absolute; top: 0px;">
</div>
<div id="divProgress" style="display: none; height: 130px; left: 0px; position: absolute;
top: 0px; width: 281px;">
<img src="/images/loading.gif">
</div>
Javascript Code
function displayProgress() {
$('#divProgress').show();
$('#divProgress').css('z-index', 2);
// this takes the id name as a parameter
Move2Center('divProgress'); }
function Move2Center(id) {
var obj = document.getElementById(id);var w1 = GetWidth(obj);
var h1 = GetHeight(obj);
var w = getWindowSize();
var scr = getScrollXY();
var left = scr[0] + parseInt(w[0] / 2) - parseInt(w1 / 2);
var top = scr[1] + parseInt(h1 / 2);
obj.style.left = left + 'px';
obj.style.top = top + 'px';
}
function getWindowSize() {
var myWidth = 0, myHeight = 0;
if (typeof (window.innerWidth) == 'number') {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
return [myWidth, myHeight];
}
GetWidth = function (elem) {
function _Convert(val) {
if (!val) { return; }
val = val.replace("px", "");
if (isNaN(val)) { return 0; }
return parseInt(val);
}
var currentStyle;
if (elem.currentStyle) { currentStyle = elem.currentStyle; }
else if (window.getComputedStyle) { currentStyle = document.defaultView.getComputedStyle(elem, null); }
else { currentStyle = elem.style; }
return (elem.offsetWidth -
_Convert(currentStyle.marginLeft) -
_Convert(currentStyle.marginRight) -
_Convert(currentStyle.borderLeftWidth) -
_Convert(currentStyle.borderRightWidth));
}
GetHeight = function (elem) {
function _Convert(val) {
if (!val) { return; }
val = val.replace("px", "");
if (isNaN(val)) { return 0; }
return parseInt(val);
}
var currentStyle;
if (elem.currentStyle) { currentStyle = elem.currentStyle; }
else if (window.getComputedStyle) { currentStyle = document.defaultView.getComputedStyle(elem, null); }
else { currentStyle = elem.style; }
return (elem.offsetHeight -
_Convert(currentStyle.marginTop) -
_Convert(currentStyle.marginBottom) -
_Convert(currentStyle.borderTopWidth) -
_Convert(currentStyle.borderBottomWidth));
}
function getScrollXY() {
var scrOfX = 0, scrOfY = 0;
if (typeof (window.pageYOffset) == 'number') {
//Netscape compliant
scrOfY = window.pageYOffset;
scrOfX = window.pageXOffset;
} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
//DOM compliant
scrOfY = document.body.scrollTop;
scrOfX = document.body.scrollLeft;
} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
//IE6 standards compliant mode
scrOfY = document.documentElement.scrollTop;
scrOfX = document.documentElement.scrollLeft;
}
return [scrOfX, scrOfY];
}
function displaySiteBlockFrame() {
divsiteframe = $("[id$=divSiteFrame]");
$(divsiteframe).show();
$(divsiteframe).css('z-index', 1);
var size = getWindowSize();
$(divsiteframe).css('width', 1000);
$(divsiteframe).css('height', $(document).height()-225);
$(divsiteframe).css('opacity', 0.7);
}
function ShowProgressBar() {
displayProgress();
displaySiteBlockFrame();
}
ASPX.CS Code:
btnfirstSearch.Attributes("onclick") = "return ShowProgressBar();
You must be logged in to post a comment.