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 单选按钮检查更改事件未触发第一个单选按钮
基础教程推荐
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01