在代码中设置自动回发控制会导致事件不触发 [英] Setting autopostback control in code causes event not to fire

查看:122
本文介绍了在代码中设置自动回发控制会导致事件不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有一组单选按钮.单选按钮位于更新面板中,每个按钮的AutoPostBack设置为true.它们都在代码隐藏页面中共享一个公共的CheckedChanged事件处理程序.我希望将特定的单选按钮设置为true时,在页面加载期间存在某些条件.当我在页面加载事件期间在代码中执行此操作时,单选按钮将不会响应CheckedChanged事件,除非我执行其他操作导致页面回发.有人知道是什么原因吗?

I have a page with a set of radio buttons. The radio buttons are in an update panel and each has AutoPostBack set to true. They all share a common CheckedChanged event handler in the code-behind page. There are certain conditions during page load when I want a particular radio button to be set to true. When I do this in code during the page load event, the radio buttons will not respond to the CheckedChanged event unless I do something else to cause the page to post back. Anyone know what''s causing this?

推荐答案

我不明白. 在服务器端,当您的代码更改任何控件的状态时,结果将不会再次发回."我预计服务器端所做的更改不会再发生回发.我在代码中处理.但是,一旦页面加载完成,并且服务器端脚本已完成,我希望发生客户端事件.单选按钮的状态更改时应回退.在某些其他控件导致回发之前,这种情况不会发生.然后,单选按钮将正常工作.
I don''t understand. "On the server side when your code changes the state of any controls, the result will not be yet another postback." I do not expect another postback from the changes made on the server side. I am handling that in code. But once the page finishes loading, and the server-side script is complete, I expect client-side events to happen. The radio buttons should postback when their state is changed. That is not happening until some other control causes a postback. Then the radio buttons work normally.


是的,原因可能是您不太了解ASP .Net页面的生命周期.

回发是由客户端HTML中嵌入的JavaScript小片段触发的请求.在服务器端,当您的代码更改任何控件的状态时,结果将不会再次发回.无论您想发生什么,您都必须通过添加执行您想发生的事情的方法来亲自查看.然后,您可以从事件处理程序中以及在进行任何不会触发其他回发的更改时调用这些方法.
Yes, the cause is probably that you don''t understand the lifecycle of ASP .Net pages very well.

Postbacks are requests that are triggered by little snippets of JavaScript embedded in the HTML on the client side. On the server side when your code changes the state of any controls, the result will not be yet another postback. Whatever you want to happen, you will have to see to it yourself by adding methods which do what you want to happen. You can then call those methods from the event handler and also when you make any changes which will not trigger another postback.


这篇关于在代码中设置自动回发控制会导致事件不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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