我有一个带有一个控件 (.ascx) 的 ASP.NET 页面。页面 (.aspx) onload 将一些文本分配给几个标签,并将产品 ID 传递给 .ascx 控件。 .ascx 控件 onload 从 .aspx 页面获取产品 ID 并多次访问数据库,进行多次计算等 - 基本上需要很长时间才能加载。
因此,当我单击指向此 .aspx 页面的链接时,页面加载需要 7-10 秒。我已将其缩小到对 .ascx 控件的计算是罪魁祸首,并且我已尽可能多地优化代码......但它仍然花费太长时间。
有没有办法在控件加载之前加载 .aspx 页面? (也许显示“正在加载...”动画?就像在 UpdateProgress 中使用的那样?)
最佳答案
您可以使用 UpdatePanel 来完成此操作。这需要一点技巧,但请尝试这样的事情:
1) 将 UserControl
放在 UpdatePanel
中。
2) 在您的用户控件上放置一个公共(public)属性,例如 IsEnabled
,它将用于有条件地不执行任何操作或呈现“请稍候”。从您的主页将其设置为 false。
3) 将 OnInit
中的一些代码添加到您的主页:
if (MyScriptManager.IsInAsyncPostback) {
MyUserControl.IsEnabled=true;
}
4) 按照这些行添加客户端脚本:
finished=false;
Sys.WebForms.PageRequestManager.pageLoaded(function(sender,args) {
if (!finished) {
finished=true;
__doPostBack('','');
// you can include the uniqueID of your updatepanel as the first arg
// otherwise it will refresh all update panels
}
});
或者使用 jquery..
finished=false;
$(document).ready(function() {
if (!finished) {...
}
});
这应该做的是在页面加载完成后立即启动异步回发,这又会导致更新面板刷新。由于您将其设置为在异步回发中启用,因此它将第二次呈现自己。
关于c# - 允许在 .ASCX 控件完成加载之前显示 .ASPX 页面吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5437001/