取消选中时,ASP.NET复选框不火的CheckedChanged事件 [英] ASP.NET CheckBox does not fire CheckedChanged event when unchecking
问题描述
我有像这样的ASP.NET内容窗体上的复选框:
I have a CheckBox on an ASP.NET Content Form like so:
<asp:CheckBox runat="server" ID="chkTest" AutoPostBack="true" OnCheckedChanged="chkTest_CheckedChanged" />
在我的code后面我有以下方式:
In my code behind I have the following method:
protected void chkTest_CheckedChanged(object sender, EventArgs e)
{
}
当我加载页面在浏览器中点击复选框它成为检查,页回,我可以看到 chkTest_CheckedChanged
被调用。
When I load the page in the browser and click the CheckBox it becomes checked, the page posts back, and I can see chkTest_CheckedChanged
being called.
当我还是那句话就变成选中单击复选框,页面回,但 chkTest_CheckedChanged
不叫。
When I then click the CheckBox again it becomes unchecked, the page posts back, however chkTest_CheckedChanged
is not called.
这个过程是可重复的,所以一旦复选框被选中,选中它会触发事件。
The process is repeatable, so once the CheckBox is unchecked, checking it will fire the event.
我有视图状态在web.config中禁用,使视图状态导致此问题消失。我能做些什么有可靠事件触发而视图状态保持禁用?
I have View State disabled in the Web.Config, enabling View State causes this issue to disappear. What can I do to have reliable event firing while the View State remains disabled?
更新:
如果我设置选中=真正的
服务器上的标签的情况变得与事件触发时,取消选中该复选框,但没有其他各地的方式逆转。
Update:
If I set Checked="true"
on the server tag the situation becomes reversed with the event firing when un-checking the CheckBox, but not the other way around.
更新2:
我重写的onLoadComplete
在我的网页,并从内部那里我可以证实,的Request.Form [__ EVENTTARGET]
正确设置为我的CheckBox的ID。
Update 2:
I've overridden OnLoadComplete
in my page and from within there I can confirm that Request.Form["__EVENTTARGET"]
is set correctly to the ID of my CheckBox.
推荐答案
实现存储自定义复选框的在经过
属性了ControlState-
,而不是的ViewState
可能会解决这个问题,即使复选框有的AutoPostBack = FALSE
Implementing a custom CheckBox that stores the Checked
property in ControlState
rather than ViewState
will probably solve that problem, even if the check box has AutoPostBack=false
不同于视图状态,了ControlState-不能被禁止,可用于存储数据是到控制的行为所必需的。
Unlike ViewState, ControlState cannot be disabled and can be used to store data that is essential to the control's behavior.
我没有一个Visual Studio环境在现在进行测试,但应该是这样的:
I don't have a visual studio environnement right now to test, but that should looks like this:
public class MyCheckBox : CheckBox
{
private bool _checked;
public override bool Checked { get { return _checked; } set { _checked = value; } }
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//You must tell the page that you use ControlState.
Page.RegisterRequiresControlState(this);
}
protected override object SaveControlState()
{
//You save the base's control state, and add your property.
object obj = base.SaveControlState();
return new Pair (obj, _checked);
}
protected override void LoadControlState(object state)
{
if (state != null)
{
//Take the property back.
Pair p = state as Pair;
if (p != null)
{
base.LoadControlState(p.First);
_checked = (bool)p.Second;
}
else
{
base.LoadControlState(state);
}
}
}
}
更多信息这里。
这篇关于取消选中时,ASP.NET复选框不火的CheckedChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!