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.
我在页面上有一个链接按钮,该按钮的click事件的事件处理器调用一个对象的方法,该方法反过来调用webservice,当webservice返回该对象时触发一个事件和一个事件处理程序在页面上codebehind更新页面上的标签。由于某种原因,标签不显示消息。
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. and the labels pre render event is being called after its text is being set by the proxy objects event handler.
Web服务是异步调用的,但是线程被阻塞直到它返回。并且在代理对象事件处理程序设置其文本之后调用标签预渲染事件。
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.
所有调用都发生在同一个线程上,我在事件处理程序中设置的任何值,而不仅仅是标签文本,都会在预渲染事件触发时恢复到之前的值。
3 个解决方案
#1
Here's what's happening:
这是发生了什么:
- 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.
用户单击您的链接按钮
浏览器向Web服务器发送回发请求
Web服务器创建页面类的全新实例并启动新的页面生命周期
页面生命周期完成所有正常的加载工作。
页面生命周期到达事件处理阶段并运行您的单击事件
click事件通过您描述的“代理”对象调用Web服务
代理对象异步调用Web服务并返回
click事件返回,因为它没有别的办法,释放ASP.Net继续在页面生命周期中移动。
页面生命周期结束,因此页面将呈现给浏览器
用户看到新页面,但标签是相同的,因为Web服务调用尚未完成
Web服务调用将返回到您的代理对象,从而引发其已完成的事件
您的事件处理程序更新已呈现的页面类中的标签文本属性
您处理此请求中的页面实例。
Update:
The OP updated his question after I posted this to indicate that the life cycle timing is kept in sync. 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. You should Invoke
the control when setting the property.
更新:OP发布后更新了他的问题,表明生命周期时间保持同步。所以相反,我会指出ASP.Net中的控件不是线程安全的,但是从后台使用的线程调用webservice completed事件来生成异步请求而不是原始线程。您应该在设置属性时调用控件。
#2
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.
如果不是,则可以在回发后重新初始化标签。
#3
Is the webservice running asynchronously? Maybe the webservice complete event fires after the page has been rendered? Have you debugged to make sure the label value is being set?
Web服务是异步运行的吗?也许webwevice完成事件在页面呈现后触发?您是否进行了调试以确保标签值已设置?
#1
Here's what's happening:
这是发生了什么:
- 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.
用户单击您的链接按钮
浏览器向Web服务器发送回发请求
Web服务器创建页面类的全新实例并启动新的页面生命周期
页面生命周期完成所有正常的加载工作。
页面生命周期到达事件处理阶段并运行您的单击事件
click事件通过您描述的“代理”对象调用Web服务
代理对象异步调用Web服务并返回
click事件返回,因为它没有别的办法,释放ASP.Net继续在页面生命周期中移动。
页面生命周期结束,因此页面将呈现给浏览器
用户看到新页面,但标签是相同的,因为Web服务调用尚未完成
Web服务调用将返回到您的代理对象,从而引发其已完成的事件
您的事件处理程序更新已呈现的页面类中的标签文本属性
您处理此请求中的页面实例。
Update:
The OP updated his question after I posted this to indicate that the life cycle timing is kept in sync. 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. You should Invoke
the control when setting the property.
更新:OP发布后更新了他的问题,表明生命周期时间保持同步。所以相反,我会指出ASP.Net中的控件不是线程安全的,但是从后台使用的线程调用webservice completed事件来生成异步请求而不是原始线程。您应该在设置属性时调用控件。
#2
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.
如果不是,则可以在回发后重新初始化标签。
#3
Is the webservice running asynchronously? Maybe the webservice complete event fires after the page has been rendered? Have you debugged to make sure the label value is being set?
Web服务是异步运行的吗?也许webwevice完成事件在页面呈现后触发?您是否进行了调试以确保标签值已设置?