[英]How to stop the javascript loader when the data retrieved from the database
I'm using a javascript with setTimeout function to display a GIF on a onclientclick of a button. 我正在使用带有setTimeout函数的javascript在按钮的onclientclick上显示GIF。 I;m trying to stop/hide the GIF once the data is retrieved from the database.
我正在尝试从数据库中检索数据后停止/隐藏GIF。 So far no luck.
到目前为止没有运气。 I have tried the following.
我尝试了以下方法。 Any help is greatly appreciated.
任何帮助是极大的赞赏。
CSS 的CSS
.modal {
position: fixed;
top: 0;
left: 0;
background-color: black;
z-index: 99;
opacity: 0.9;
filter: alpha(opacity=80);
-moz-opacity: 0.9;
min-height: 100%;
width: 100%;
}
.loading {
font-family: Arial;
font-size: 10pt;
width: 200px;
height: 100px;
display: none;
position: fixed;
opacity: 0.9;
z-index: 999;
}
Javascript Java脚本
function ShowLoader() {
if (Page_ClientValidate('DownloadReport')) {
setTimeout(function() {
var modal = $('divLoader');
modal.addClass('modal');
$('body').append(modal);
var loading = $('.loading');
loading.show();
var top = Math.max($(window).height() / 2 - loading[0].offsetHeight / 2, 0);
var left = Math.max($(window).width() / 2 - loading[0].offsetWidth / 2, 0);
loading.css({
top: top,
left: left
});
}, 100);
} else {
}
};
function HideLoader() {
document.getElementById('<%= divLoader.ClientID%>').style.visibility = "hidden";
};
so on button click I call the showLoader, 在按钮上单击“我调用showLoader”,
<asp:Button ID="btnGenerateRep" CssClass="btnGenerateRep" runat="server" Text="Download Report" ValidationGroup="DownloadReport" OnClientClick="ShowLoader();" OnClick="btnGenerateRep_Click" />
And I want to stop it when data is retrieved from DB 我想从数据库中检索数据时停止它
C# code, C#代码
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
if (dt.Rows.Count > 0) {
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "HideLoader()", true);
string dateTimeStamp = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
string excelFileName = aliasName + "-" + dateTimeStamp + ".xlsx";
string worksheetsName = "Sheet";
var format = new ExcelTextFormat();
format.Delimiter = ',';
format.EOL = "\r";
using(ExcelPackage package = new ExcelPackage()) {
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(worksheetsName);
worksheet.Cells["A1"].LoadFromDataTable(dt, true);
Response.Clear();
package.SaveAs(Response.OutputStream);
Response.AddHeader("content-disposition", "attachment; filename=" + excelFileName + ";");
Response.Charset = "";
Response.ContentType = "application/vnd.xlsx";
Response.End();
}
}
I'm calling the javascript from C# code, but it's not firing. 我正在从C#代码中调用javascript,但是没有触发。 Or can I capture the Save command when the SaveAs dialog box comes up from package.SaveAs and then call the javascript from there?
还是从package.SaveAs出现“另存为”对话框时我可以捕获“保存”命令,然后从那里调用JavaScript?
I have also tried hide the asp:Panel where the GIF resides when data is received. 我还尝试了在接收数据时隐藏GIF所在的asp:Panel。 Still no luck.
仍然没有运气。 I would really appreciate any help.
我真的很感谢您的帮助。 Thanks a lot
非常感谢
As first move 第一步
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "HideLoader()", true);
out from the if (dt.Rows.Count > 0) { ... }
从
if (dt.Rows.Count > 0) { ... }
because this means if there are 0
rows it will never add the script so it wil never get executed clientside so it wil also never hide the loading. 因为这意味着如果有
0
行,它将永远不会添加脚本,因此永远不会在客户端执行脚本,因此也永远不会隐藏加载。
As second test HideLoader and see if you get a alert. 作为第二项测试HideLoader,看看是否收到警报。
function HideLoader() {
alert('test');
};
If you get a alert it means it gets called. 如果收到警报,则表示它被调用。 After that add your own line back and check if that works.
之后,添加您自己的行,然后检查是否可行。
Also since you are using jquery.show maybe also just use same way to hide it? 另外由于您使用的是jquery.show,也许也只是使用相同的方式来隐藏它? Check this to change that if needed.
选中此按钮可以根据需要进行更改。
$('.loading').hide();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.