ASP.NET Radio button checked changed event not firing for first radio button(ASP.NET 单选按钮检查更改事件未触发第一个单选按钮)
问题描述
我面临一个问题,即第一个单选按钮的选中更改事件未触发.我启用了 ViewState 但问题仍然存在.请看以下代码:
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. 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>
我在 Page_Init 上设置选中的更改事件,如下所示:
And I am setting the checked changed event on Page_Init as below:
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 事件会成功触发.但是,我需要在页面加载时默认选中第一个单选按钮.
One thing I noticed is when I remove the Checked="true" property on the first radio button the CheckedChanged event fires successfully. However, I need the first radio button to be checked by default on page load.
推荐答案
最初你可以为所有的 RadioButtons 保留 Checked="false",然后用客户端代码设置选中的按钮:
You can leave Checked="false" for all the RadioButtons initially, and set the selected button with client code:
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);
}
单击任何 RadioButton 将始终触发 CheckedChanged 事件.实际选中的 RadioButton 存储在 selectedRadioButton 中,如果您在服务器代码的其他部分需要它.
Clicking on any RadioButton will always trigger the CheckedChanged event. The RadioButton that is actually selected is stored in selectedRadioButton, if you need it in other parts of the server code.
这篇关于ASP.NET 单选按钮检查更改事件未触发第一个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASP.NET 单选按钮检查更改事件未触发第一个单选按钮
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
