ASP.net无法从事件处理程序更新页面 [英] ASP.net can't update page from event handler

查看:69
本文介绍了ASP.net无法从事件处理程序更新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有一个linkbutton,该按钮的click事件的事件处理程序调用对象上的方法,该对象又对webservice进行调用,当webservice返回该对象时,将触发一个事件并触发一个事件处理程序页面上的代码后面的代码将更新页面上的标签.由于某种原因,标签不显示消息.

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. For some reason the label doesn't display the message.

这一直让我发疯,我在Google上找不到任何有关它的信息.任何帮助都会得到应用.

This has been driving me crazy and i can't find anything about it on google. any help would be appriciated.

正在异步调用Web服务,但是线程被阻塞,直到返回为止.并在代理对象事件处理程序设置了其文本之后调用了标签pre render事件.

The webservice is being called asynchronously but the thread is being blocked until it returns. 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.

推荐答案

这是正在发生的事情:

  • 用户单击您的链接按钮
  • 浏览器将回发请求发送到Web服务器
  • 网络服务器会为您的页面类创建一个全新的实例,并开始一个新的页面生命周期
  • 页面生命周期完成所有正常的加载工作.
  • 页面生命周期到达事件处理阶段并运行您的click事件
  • click事件通过您描述的代理"对象调用Web服务
  • 代理对象异步调用Web服务并返回
  • 点击事件返回,因为它没有其他事情要做,因此释放了ASP.Net,可以继续在页面生命周期中移动.
  • 页面生命周期结束,因此页面呈现给浏览器
  • 用户看到新页面,但是标签相同,因为Web服务调用尚未结束
  • Web服务调用返回到您的代理对象,这引发了它的完成事件
  • 您的事件处理程序更新页面类中已呈现的标签的text属性
  • 您处理此请求的页面实例.
  • The user clicks your link button
  • The browser sends a postback request to the web server
  • The web server creates a brand new instance of your page class and starts a new page life cycle
  • 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
  • The proxy object calls the web service asynchronously and returns
  • The click event returns, because it has nothing else to do, freeing up ASP.Net to continue moving through the page life cycle.
  • 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
  • Web service call returns to your proxy object, which raises its completed event
  • Your event handler updates the label's text property in your page class which was already rendered
  • Your page instance from this request is disposed.

更新:
我发布此消息后,OP更新了他的问题,以指示生命周期计时保持同步.因此,我要指出的是,ASP.Net中的控件不是线程安全的,但是从后台使用的线程中调用webservice complete事件是为了发出异步请求,而不是发起线程.设置属性时,您应该调用控件.

这篇关于ASP.net无法从事件处理程序更新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆