>我遇到了第一个单选按钮的已检查更改事件未触发的问题。我启用了ViewState
但问题仍然存在。请参阅下面的代码:
<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>
我正在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();
};
}
我注意到的一件事是,当我删除第一个单选按钮上的 Checked="true"
属性时,CheckedChanged
事件成功触发。但是,我需要在页面加载时默认检查第一个单选按钮。
您可以最初为所有单选按钮保留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);
}
单击任何单选按钮将始终触发CheckedChanged
事件。实际选择的单选按钮存储在selectedRadioButton
中,如果您需要它在服务器代码的其他部分中。