asp.net – 问题检查后面的代码中的单选按钮
发布时间:2020-12-16 04:10:20 所属栏目:asp.Net 来源:网络整理
导读:我有一个简单的ASP.NET表单,带有DropDownList和两个RadioButtons(两者都共享相同的GroupName). 在DropDownList的SelectedIndexChanged事件中,我在两个RadioButtons上设置Checked = true. 它设置第二个RadioButton很好,但它不会检查第一个.我究竟做错了什么?
我有一个简单的ASP.NET表单,带有DropDownList和两个RadioButtons(两者都共享相同的GroupName).
在DropDownList的SelectedIndexChanged事件中,我在两个RadioButtons上设置Checked = true. 它设置第二个RadioButton很好,但它不会检查第一个.我究竟做错了什么? <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <body> <form id="form1" runat="server"> <asp:DropDownList runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_Changed" ID="ddl"> <asp:ListItem Text="Foo" /> <asp:ListItem Text="Bar" /> </asp:DropDownList> <asp:RadioButton runat="server" ID="rb1" Text="Foo" GroupName="foobar" /> <asp:RadioButton runat="server" ID="rb2" Text="Bar" GroupName="foobar" /> </form> </body> </html> protected void ddl_Changed(object sender,EventArgs e) { if (ddl.SelectedIndex == 0) rb1.Checked = true; // <- Doesn't actually work else rb2.Checked = true; } 解决方法
它失败了,因为它试图将它们都设置为选中,这对于组中的无线电按钮是不可能的.
最好的解决方案是使用RadioButtonList: <asp:RadioButtonList ID="rblTest" runat="server"> <asp:ListItem Text="Foo"></asp:ListItem> <asp:ListItem Text="Bar"></asp:ListItem> </asp:RadioButtonList> 然后像这样设置所选项目: protected void ddl_Changed(object sender,EventArgs e) { rblTest.ClearSelection(); rblTest.SelectedIndex = ddl.SelectedIndex; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 在没有模型的情况下手动将验证添加到文本框
- 在Azure中设置ASP.NET 5 Web应用程序的SQL连接字符串
- asp.net-mvc – LINQ将DateTime转换为字符串
- asp.net-mvc – 从MVC5框架OAuth/OWin身份提供程序与外部au
- ASP.NET网站 – > WCF服务 – > WCF服务,一路冒充?
- 如何缓存输出的action方法,返回图像到asp.net mvc中的视图?
- asp.net – Visual Basic以编程方式将用户名和密码传递给ht
- asp.net-mvc – ASP.NET Web优化 – 无需捆绑即可缩小
- asp.net-mvc-2 – Plus()在MVC中的参数引起404在IIS 7.0
- asp.net – 将default.aspx重定向到根虚拟目录
推荐文章
站长推荐
- asp.net – 获取在page_init中触发回发的控件
- 如何让OleDb在asp.net核心项目中阅读excel
- asp.net – 文件 – >在MonoDevelop中打开网站?
- asp.net-mvc – 自定义DataAnnotations Validato
- asp.net-mvc – 实用程序方法可以在MVC剃刀中返回
- asp.net – System.Web.Security.FormsAuthentic
- asp.net – 尝试的转移似乎包含数据泄漏?
- 只需发布已修改的控制器,模型和ASP.NET MVC项目的
- ASP.NET中内联标记的全球化差异
- asp.net-mvc-3 – 在脚手架视图MVC3中进行分页
热点阅读