我是否完全误解了 ASP.Net AJAX(更新面板)?

发布于 2024-10-05 00:20:06 字数 494 浏览 2 评论 0原文

我可能误解了 AJAX 的工作原理 - 有人可以解释一下以下简化场景:

我有一个 asp.net Web 应用程序。在页面上,更新面板内有一个用户控件(没有更改属性)和一个脚本管理器。

在用户控件和窗体上有一个标签,两者都在加载事件中将其文本设置为 DateTime.Now.ToString。还有一个按钮,它会导致用户控件中的回发。

当我单击该按钮时,正如我所期望的那样,用户控件内的标签会更新,并且页面上的一个标签现在也会更新。到目前为止,一切都很好。

但是...页面上的页面加载事件确实使用 isPostBack = True 进行处理(这是我没想到的),并且看起来加载事件中发生的任何事情都不会被推回到客户端(因为标签没有更新)。

我没想到在更新 AJAX 面板时会引发并处理页面加载事件(在包含用户控件的页面中),这是正确的吗?或者我做错了什么?我记得读过一些有关 Page.IsCallback 的内容,但那是错误的,所以也许这与此无关。

I've may have misunderstood how AJAX works - Can someone shed some light on the following simplified scenario:

I have a asp.net web application. On a page there is a user control inside an update panel (with no properties changed) and a script manager.

In the user control and on the form there is the a label, both get their text set to DateTime.Now.ToString in the load event. There is also a button, which causes a post back in the user control.

When I click the button, as I expect the label inside the user control updates and the one label on the page does now. So far so good.

However... the page load event on the page does get processed with isPostBack = True (which I did not expect), and it looks like whatever happens in the load event doesn't get pushed back to the client (as the label didn't update).

I didn't expect the page load event (in the page that contains the user control) to be raised and processed when an AJAX panel is updated, is this correct? or am I doing something wrong? I remember reading something about Page.IsCallback, but that is false, so maybe that has nothing to do with this.

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

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

发布评论

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

评论(4

妖妓 2024-10-12 00:20:10

如果没有看到您正在做的事情的一些示例,我认为无法判断您是否做错了什么。错误

但是,是的,更新面板仍然调用服务器端的大部分页面生命周期。
如果您正在寻找的是避免页面生命周期,那么页面方法可能更符合您的口味。不过,它们确实需要使用 javascript。

http://forums.asp.net/p/1070297/1571597.aspx

Without seeing some samples of what you're doing, I don't think it's possible to say if you're doing something wrong.

But, yes, Update panels still invoke most of the page life cycle on the server side.
If what you're looking for is to avoid the page lifecycle, page methods may be more to your taste. They do require mucking around with javascript though.

http://forums.asp.net/p/1070297/1571597.aspx

浪荡不羁 2024-10-12 00:20:10

在回发期间,所有页面生命周期事件都会再次执行,页面会重新创建并重新发送给客户端。

UpdatePanel 本质上执行完整的回发,但只有更新面板内的控件会在客户端上刷新。因此,更新面板之外的任何控件都不会更新,即使您可能在服务器上更改它们的值。

您可以使用 Page.IsPostBack 属性来检查您是否执行页面的初始加载或回发。例如,只在页面第一次加载时执行某些操作:

if (!Page.IsPostBack)
{
    //doSomething
}

During a PostBack all the page life cycle events get executed again and the page is recreated and resent to the client.

An UpdatePanel essentially does a full postback but only the controls inside the update panel are refreshed on the client. So any controls outside the update panel will not update even though you might change their value on the server.

You can use the Page.IsPostBack property to check whether you doing the initial loading of the page or a postback. eg to only do something the first time the page loads:

if (!Page.IsPostBack)
{
    //doSomething
}
可可 2024-10-12 00:20:09

即使在部分回发时,所有页面生命周期事件也会执行。您可以通过执行以下操作来区分完整回发和部分回发:

   if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
   {

   }

All of the page lifecycle events get executed even on partial postbacks. You can differentiate between a full postback and a partial postback by doing the following:

   if (ScriptManager.GetCurrent(this).IsInAsyncPostBack)
   {

   }
掌心的温暖 2024-10-12 00:20:07

嗯,这个问题不是关于 AJAX 本身,而是关于 Microsoft 的基于 AJAX 的 UpdatePanel,它是一个复杂的野兽。 UpdatePanel 工作方式的简单解释是,一切都与正常的全页面“回发”相同(ViewState POST 到服务器,重新创建服务器端 DOM,执行所有页面事件生命周期事件)除了在最后呈现给客户端的响应仅包含刷新发起 AJAX 请求的 UpdatePanel 内容所需的 HTML 子集。虽然还有一些额外的微妙之处和复杂性,但这是基本思想。

Well, this question is isn't about AJAX per-se, but about Microsoft's AJAX-based UpdatePanel, which is a complex beast. The simple explanation of the way UpdatePanel's work is that everything works the same as a normal full-page "post back" (ViewState POSTed to the server, server-side DOM is recreated, all of the page event life-cycle events are executed) except at the very end the response rendered to the client only includes the subset of HTML needed to refresh the content of the UpdatePanel from which the AJAX request was initiated. There are some additional subtleties and complexities at play, but this is the basic idea.

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