我有一个asp页面。 在第一次加载页面时,函数Page_Load需要很长时间才能完成(15秒)而且我不能缩短它。
我想实现这个目标:
页面首先显示的是,在所有内容之前,是一个带有loading字样的div。然后在Page_Load完成后我想让它消失。
如何实现这种行为?
答案 0 :(得分:0)
您可以拥有加载消息面板和内容面板。 这将允许Page首先呈现加载消息,然后调用长请求的请求
void Page_Load(){
var fromMsg = Request["loading"] == "1";
if (!fromMsg) {
LoadingPanel.Visible = true;
ContentPanel.Visible = false
} else {
// Load Heavy Content Here
}
}
然后在您的加载面板中添加一些javascript来调用重新加载
<asp:Panel id="LoadingPanel" runat="server" Visible="false">
<h2>Loading...</h2>
<script type='javascript'>
window.setTimeout(function(){
location.href = location.href += "&loading=1";
}, 400);
</script>
</asp:Panel>
<asp:Panel id="ContentPanel" runat="server">
...
</asp:Panel>
如果加载消息很简单,你可以使用像
这样简单的东西在CodeBehind中完成同样的事情void OnInit()
{
var fromMsg = Request["loading"] == "1";
if (!fromMsg) {
Response.Clear();
Response.Write("<h2>Loading....</h2>");
Response.Write("<script type='text/javascript'>window.setTimeout(function(){ location.href = 'ThisPage.aspx?loading=1'; }, 400);
Response.End();
}
}
如果您需要在很多页面上执行此操作,您可以创建一个Helper库来生成MessageHtml并使用ScriptManager设置刷新JS