[英]ASP.NET Radio button checked changed event not firing for first radio button
I am facing an issue where the checked changed event of the first radio button is not firing. 我面临的问题是第一个单选按钮的已检查更改事件未触发。 I enabled ViewState
but still the issue persists. 我启用了ViewState
但问题仍然存在。 Please see below code: 请看下面的代码:
<span class="pull-right text-right">
<label class="inline radio">
<asp:RadioButton runat="server" ID="rdoViewAll" CausesValidation="false" GroupName="Filter" Text="View All" AutoPostBack="true" EnableViewState="true" Checked="true" />
</label>
<label class="inline radio">
<asp:RadioButton runat="server" ID="rdoViewCurrent" CausesValidation="false" GroupName="Filter" Text="View Current" AutoPostBack="true" />
</label>
<label class="inline radio">
<asp:RadioButton runat="server" ID="rdoViewFuture" CausesValidation="false" GroupName="Filter" Text="View Future" AutoPostBack="true" />
</label>
</span>
And I am setting the checked changed event on Page_Init
as below: 我在Page_Init
上设置了已检查的已更改事件,如下所示:
public void Page_Init(object sender, EventArgs e)
{
this.rdoViewAll.CheckedChanged += (s, a) =>
{
RebindTerms();
};
this.rdoViewFuture.CheckedChanged += (s, a) =>
{
RebindTerms();
};
this.rdoViewCurrent.CheckedChanged += (s, a) =>
{
RebindTerms();
};
}
One thing I noticed is when I remove the Checked="true"
property on the first radio button the CheckedChanged
event fires successfully. 我注意到的一件事是当我删除第一个单选按钮上的Checked="true"
属性时, CheckedChanged
事件成功触发。 However, I need the first radio button to be checked by default on page load. 但是,我需要在页面加载时默认选中第一个单选按钮。
You can leave Checked="false"
for all the RadioButtons initially, and set the selected button with client code: 您最初可以为所有RadioButton留下Checked="false"
,并使用客户端代码设置所选按钮:
private RadioButton selectedRadioButton;
protected void Page_Load(object sender, EventArgs e)
{
selectedRadioButton = rdoViewAll;
if (rdoViewCurrent.Checked)
{
selectedRadioButton = rdoViewCurrent;
}
if (rdoViewFuture.Checked)
{
selectedRadioButton = rdoViewFuture;
}
rdoViewAll.Checked = false;
rdoViewCurrent.Checked = false;
rdoViewFuture.Checked = false;
ClientScript.RegisterStartupScript(GetType(), "InitRadio", string.Format("document.getElementById('{0}').checked = true;", selectedRadioButton.ClientID), true);
}
Clicking on any RadioButton will always trigger the CheckedChanged
event. 单击任何RadioButton将始终触发CheckedChanged
事件。 The RadioButton that is actually selected is stored in selectedRadioButton
, if you need it in other parts of the server code. 实际选择的RadioButton存储在selectedRadioButton
,如果您需要在服务器代码的其他部分中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.