ASP.Net 的自动回发。当速度太慢时会发生什么?

发布于 2024-08-10 18:40:44 字数 599 浏览 1 评论 0原文

我正在制作一个网络应用程序。我在更新面板时遇到了一个奇怪的错误。

好的,假设您有两个更新面板,每个更新面板中都有一个文本框。这两个文本框都是自动回发的,并且更新面板有条件地更新。

好吧,从我观察到的行为来看,如果服务器处理请求的速度不比用户快,那么它就会在客户端被忽略。 比如说,您在其中一个文本框中输入一些内容,然后快速跳到下一个文本框,输入一些内容并跳出。这应该会导致 2 次回发。

那么,如果服务器正在处理 1 个回发,而另一个回发发生了怎么办?该回发是否会在服务器端或客户端被丢弃?

我在这种情况下观察到的主要问题是,当第一次发生回发时,更新面板有一个 Update() 。好吧,当第二个回发发生中断第一个回发时,它还会在更新面板(不同的面板)上进行更新。用户看到的是,如果他们非常快速地浏览它(或者服务器处于高负载或其他情况下),那么第二个更新面板会更新,但不是第一个。

tl;dr: 当一个回发中断另一个回发时,任何本应在第一个回发中更新的更新面板都不会更新(尽管第二个回发会更新)

如何我解决这个问题还是解决它? 我无法更新屏幕上的所有更新面板,因为这样用户当前所在的控件就会失去焦点以及许多其他问题。

I am making a web application. I have gotten a weird error with update panels.

Ok, so say you have two update panels and each update panel has a textbox in it. Both of these textboxes are auto-postback and the update panels update conditionally.

Well, from the behavior I'm observing it seems like if the server isn't faster than the user at processing a request then it sorta gets ignored on the client side.
Like say you type something in 1 of these text boxes and then quickly tab to the next one and type something and tab out. This should cause 2 post backs.

Well, what if 1 post back is being processed at the server and another one happens? Does that post back get dropped at the server side or client side?

The main problem I'm observing with this situation is that when a post back occurs the 1st time, there is a Update() for an update panel. Well, when the 2nd post back occurs interrupting the first, it also does an Update on an update panel(a different one). What the user sees is if they tab through it very quickly(or the server is under high load or whatever) then the 2nd update panel gets updated but not the first.

tl;dr: When a post back interrupts another post back, any update panels that were suppose to be updated in the first post back are not updated(though the second postback ones are)

How can I work around this problem or solve it? I can not update all of the update panels on the screen because then the control that the user is currently on loses focus along with a whole lot of other problems.

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

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

发布评论

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

评论(3

红ご颜醉 2024-08-17 18:40:44

UpdatePanels 拦截回发并使用 XMLHTTPRequest 对象向服务器发出请求(即它们使用 AJAX)。

如果在发出第二个 XMLHTTPRequest 时第一个 XMLHTTPRequest 仍在进行中,则第二个 XMLHTTPRequest 将取消第一个 XMLHTTPRequest。据我所知,这是标准行为。

您可能希望在单击按钮时一起更新 UpdatePanels,而不是将更新附加到每个文本框上的事件(听起来像是将它们附加到 blur 事件)。通过这种方式,您可以确保不会发出大量请求,并且可以在请求正在进行时禁用该按钮,以防止新请求取消正在进行的请求。

编辑:

您可以通过检查 PageRequestManagerisInAsyncPostBack 属性来防止在一个请求已在进行时从客户端发出另一请求。类似下面的内容

function pageLoad(sender, args) {

var pageManager = Sys.WebForms.PageRequestManager.getInstance();

// add a function to execute when an asynchronous postback is initialized
pageManager.add_initializeRequest(checkAsyncPostback);

} 

function checkAsyncPostback(sender, arg)
{
    var pageManager = Sys.WebForms.PageRequestManager.getInstance();
    // check if an async postback is already in progress
    if (pageManager.get_isInAsyncPostBack()) {
        // cancel this async postback if one is currently in progress
        arg.set_cancel(true);
    }
}

实际上没有一种简单的方法可以从服务器端知道回发是否中断。

UpdatePanels intercept the postback and make a request back to the server using the XMLHTTPRequest object (i.e. they use AJAX).

The second XMLHTTPRequest will cancel the first one if it is still in progress when the second one is made. This is standard behaviour as far as I am aware.

You might want to have the UpdatePanels update together on a button click as opposed to having the update attached to an event on each textbox (it sounds like you have them attached to the blur event). This way you can ensure that lots of requests aren't being made and perhaps disable the button whilst a request is in progress, to prevent a new request from cancelling the one in progress.

EDIT:

You can prevent another request being made form the client side while one request is already in progress by checking the PageRequestManager's isInAsyncPostBack property. Something like the following

function pageLoad(sender, args) {

var pageManager = Sys.WebForms.PageRequestManager.getInstance();

// add a function to execute when an asynchronous postback is initialized
pageManager.add_initializeRequest(checkAsyncPostback);

} 

function checkAsyncPostback(sender, arg)
{
    var pageManager = Sys.WebForms.PageRequestManager.getInstance();
    // check if an async postback is already in progress
    if (pageManager.get_isInAsyncPostBack()) {
        // cancel this async postback if one is currently in progress
        arg.set_cancel(true);
    }
}

There isn't really an easy way to know from the server side if the postback is interrupted.

傻比既视感 2024-08-17 18:40:44

我无法具体回答您的问题,因为我不知道页面如何管理 UpdatePanel 及其请求/响应。但是,如果您使用 Fiddler 跟踪调用,您可能可以很容易地知道发生了什么。您可以看到请求何时触发,还可以看到响应以及是否发回 HTTP 错误代码或引发异常等:

Fiddler2

I can't answer your questions specifically, because I don't know how the page manages UpdatePanels and their requests/responses. But, you can probably very easily tell what's going on if you trace the calls with Fiddler. You can see when the requests are firing, and you can see the response as well as if an HTTP error code is sent back or an exception is thrown, etc:

Fiddler2

白云悠悠 2024-08-17 18:40:44

查看如何使用 UpdateProgress 控件: http://www.asp.net /ajax/documentation/live/overview/UpdateProgressOverview.aspx

另外,UpdatePanel 对于这种事情来说太过分了。查看使用页面方法(google ASP.NET 页面方法)

编辑:为了进一步阐明这对您有何用处,请修改 UpdateProgress 控件中的 ProgressTemplate 的内容以模式方式显示,以便用户无法执行任何操作,直到请求完成。

Look at using the UpdateProgress control: http://www.asp.net/ajax/documentation/live/overview/UpdateProgressOverview.aspx

Also, UpdatePanels are overkill for this kind of thing. Look at using Page Methods (google ASP.NET Page Methods)

EDIT: To further clarify how this would be useful to you, modify the contents of the ProgressTemplate in the UpdateProgress control to display in a modal fashion, so that users cannot do anything until the request completes.

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