简体   繁体   English

ASP.net 无法从事件处理程序更新页面

[英]ASP.net can't update page from event handler

i have a linkbutton on a page, the event hander for the click event of that button calls a method on an object which in turn makes a call to a webservice, when the webservice returns the object fires an event and an event handler on the page codebehind updates a label on the page.我在页面上有一个链接按钮,该按钮的单击事件的事件处理程序调用 object 上的一个方法,该方法反过来调用一个 Web 服务,当 Web 服务返回 object 时会在页面上触发一个事件和一个事件处理程序代码隐藏更新页面上的 label。 For some reason the label doesn't display the message.由于某种原因,label 不显示该消息。

This has been driving me crazy and i can't find anything about it on google.这一直让我发疯,我在谷歌上找不到任何关于它的信息。 any help would be appriciated.任何帮助都会得到帮助。

The webservice is being called asynchronously but the thread is being blocked until it returns.正在异步调用 Web 服务,但线程被阻塞,直到它返回。 and the labels pre render event is being called after its text is being set by the proxy objects event handler.并且标签预渲染事件在其文本由代理对象事件处理程序设置后被调用。

All the calls are happening on the same thread, any values i set,not just the label text, in the event handler are getting reverted to their previous values by the time the pre-render event fires.所有调用都发生在同一个线程上,我设置的任何值,而不仅仅是 label 文本,在预渲染事件触发时,事件处理程序中的值都将恢复为之前的值。

Here's what's happening:这是正在发生的事情:

  • The user clicks your link button用户点击您的链接按钮
  • The browser sends a postback request to the web server浏览器向 web 服务器发送回发请求
  • The web server creates a brand new instance of your page class and starts a new page life cycle web 服务器创建一个全新的页面实例 class 并开始新的页面生命周期
  • The page life cycle does all it's normal load work.页面生命周期完成所有正常的加载工作。
  • The page life cycle reaches the event handling stage and runs your click event页面生命周期到达事件处理阶段并运行你的点击事件
  • The click event invokes the web service via the "proxy" object you described点击事件通过您描述的“代理”object 调用 web 服务
  • The proxy object calls the web service asynchronously and returns代理object异步调用web服务并返回
  • The click event returns , because it has nothing else to do, freeing up ASP.Net to continue moving through the page life cycle. click 事件返回,因为它无事可做,从而释放 ASP.Net 以继续在页面生命周期中移动。
  • The page life cycle finishes, so the page is rendered to the browser页面生命周期结束,所以页面被渲染到浏览器
  • The user sees the new page, but the label is the same because the web service call hasn't finished yet用户看到新页面,但label 是一样的,因为 web 服务调用尚未完成
  • Web service call returns to your proxy object, which raises its completed event Web 服务调用返回到您的代理 object,这会引发其完成的事件
  • Your event handler updates the label's text property in your page class which was already rendered您的事件处理程序更新已呈现的页面 class 中标签的文本属性
  • Your page instance from this request is disposed.此请求中的页面实例已被释放。

Update:更新:
The OP updated his question after I posted this to indicate that the life cycle timing is kept in sync. OP 在我发布此问题后更新了他的问题,以表明生命周期时间保持同步。 So instead, I'll point out that controls in ASP.Net are not threadsafe, but the webservice completed event is called from the thread used in the background to make the asynchronous request rather than the originating thread.因此,相反,我将指出 ASP.Net 中的控件不是线程安全的,而是从后台使用的线程调用 webservice 完成事件以发出异步请求,而不是从源线程调用。 You should Invoke the control when setting the property.您应该在设置属性时Invoke控件。

Have you got this in your Page_Load event?你在你的 Page_Load 事件中有这个吗?

if (Page.IsPostBack)
{
   return;
}

If not, the label can be re-initialized after the postback.如果没有,label 可以在回发后重新初始化。

Is the webservice running asynchronously?网络服务是否异步运行? Maybe the webservice complete event fires after the page has been rendered?页面渲染后可能会触发 web 服务完成事件? Have you debugged to make sure the label value is being set?您是否已调试以确保正在设置 label 值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM