允许在 .ASCX 控件完成加载之前显示 .ASPX 页面吗?

发布于 2024-10-26 07:11:37 字数 347 浏览 4 评论 0原文

我有一个 ASP.NET 页面,上面有一个控件 (.ascx)。页面 (.aspx) onload 将一些文本分配给几个标签,并将产品 ID 传递给 .ascx 控件。 .ascx 控件 onload 从 .aspx 页面获取该产品 ID 并多次访问数据库,进行多次计算等 - 基本上需要很长时间才能加载。

因此,当我单击此 .aspx 页面的链接时,该页面的加载时间为 7-10 秒。我已经将其范围缩小到 .ascx 控件上的计算是罪魁祸首,并且我已经尽可能地优化了代码......但它仍然花费了太长时间。

有没有办法在控件加载之前加载 .aspx 页面? (也许显示“正在加载...”动画?就像在 UpdateProgress 中使用的那样?)

I have an ASP.NET page with one control (.ascx) on it. The page (.aspx) onload assigns some text to a couple labels and passes a product ID to the .ascx control. The .ascx control, onload, takes that product ID from the .aspx page and hits the database several times, does several calculations, etc - basically takes a long time to load.

So when I'm clicking a link to this .aspx page, it is taking 7-10 seconds for the page to load. I've narrowed it down to the calculations on the .ascx control being the culprit and I've optimized the code as much as I can ... but it's still taking too long.

Is there a way to load the .aspx page BEFORE the control loads? (Maybe display a "Loading..." animation? Like used in an UpdateProgress?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开柳相依 2024-11-02 07:11:37

您可以使用 UpdatePanel 来完成此操作。这需要一些技巧,但请尝试如下操作:

1) 将UserControl 放入UpdatePanel 中。

2) 在用户控件上放置一个公共属性,例如 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) {...
   }
});

这应该做的是在页面之后立即启动异步回发加载完成,这将导致更新面板刷新。由于您将其设置为在异步回发时启用,因此它将在第二次呈现自身。

You could do this with an UpdatePanel. It will take a little trickery, but try something like this:

1) Put the UserControl in an UpdatePanel.

2) Put a public property on your usercontrol like IsEnabled that it will use to conditionally do nothing or render a "please wait." Set it false from your main page.

3) Add some code in OnInit to your main page:

if (MyScriptManager.IsInAsyncPostback) {
    MyUserControl.IsEnabled=true;
}

4) Add a client script along these lines:

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
    }
});

or with jquery..

finished=false;
$(document).ready(function() {
   if (!finished) {...
   }
});

What this should do is cause an async postback to be initiated immediately after the page is done loading, which will in turn cause the update panel to be refreshed. Since you set it to be enabled when it's in an async postback, it will render itself the 2nd time.

如果没有你 2024-11-02 07:11:37

实现此目的的唯一可能方法是将其设置为单独的 HTTP 资源。目前,.NET 正在将控件集成到页面中,以便它等待,直到拥有响应所需的所有内容。

您可以通过多种不同的方式来执行此操作:

  • 通过 javascript 调用的 Web 服务
  • 包含控件的单独页面(并且托管在 iFrame 中以显示在同一页面上)

执行此操作的最佳方法是使用iFrame(或类似的东西)将指示浏览器在发送主页后请求控件)。

The only possible way to achieve this is by setting it up as a separate HTTP resource. At the moment .NET is integrating the control into the page so that it is waiting unti it has everything it needs to respond.

You could do this a multitude of different ways:

  • Web Service that gets called via javascript
  • Seperate page which contains the control (and is hosted within an iFrame to appear to be on the same page)

The best way to do this would be to use an iFrame (or something similar) which will instruct the browser to request the control after the main page has been sent).

貪欢 2024-11-02 07:11:37

就我个人而言,我永远不会使用 iFrame 来加载页面上的内容 - 这更像是一种黑客行为,而且 iframe == “坏”。

但他们是对的,你将无法做任何你正在寻找的事情。

如果用户控件没有任何导致回发的 Web 控件(或者具有在回发期间需要访问的任何表单控件),那么我将在页面加载并使用后使用 AJAX 来请求服务器上的数据javascript来显示页面上的内容。

Personally, I would never use an iFrame to load content on a page - that's more like a hack than anything and plus, iframe == "bad".

But they are right, you won't be able to do anything like what you're looking for.

If the user control DOES NOT have any web controls that cause a postback (or have any form controls that you need to access during a postback), then I would use AJAX to request the data on the server after the page has already loaded and use javascript to display the content on the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文