判断是否有选择RadioButtonList
写Javascript来判断是否有对RadioButtonList选项选择,效果如下:
准备好RadioButtonList数据源:

Imports Microsoft.VisualBasic Namespace Insus.NET Public Class Cosmetic Private _ID As Integer Private _Type As String Private _Name As String Private _Weight As Decimal Private _UM As String Public Property ID As Integer Get Return _ID End Get Set(value As Integer) _ID = value End Set End Property Public Property Type As String Get Return _Type End Get Set(value As String) _Type = value End Set End Property Public Property Name As String Get Return _Name End Get Set(value As String) _Name = value End Set End Property Public Property Weight As Decimal Get Return _Weight End Get Set(value As Decimal) _Weight = value End Set End Property Public Property UM As String Get Return _UM End Get Set(value As String) _UM = value End Set End Property Public Sub New() End Sub Public Sub New(id As Integer, type As String, name As String, weight As Decimal, um As String) Me._ID = id Me._Type = type Me._Name = name Me._Weight = weight Me._UM = um End Sub Public Function GetData() As List(Of Cosmetic) Dim o As New List(Of Cosmetic) Dim c As New Cosmetic(1, "滋润霜", "玉兰油", 50, "g") o.Add(c) Dim c1 As New Cosmetic(2, "滋润霜", "雅诗兰黛", 100, "g") o.Add(c1) Dim c2 As New Cosmetic(3, "滋润霜", " 兰蔻", 80, "g") o.Add(c2) Dim c3 As New Cosmetic(4, "滋润霜", "欧莱雅", 60, "g") o.Add(c3) Dim c4 As New Cosmetic(5, "滋润霜", "芭比波朗", 120, "g") o.Add(c4) Return o End Function End Class End Namespace
在aspx放一个RadioButtonList控件和一个铵钮:
1 化妆品:<asp:RadioButtonList ID="RadioButtonListCosmetic" runat="server" RepeatColumns="10" RepeatDirection="Horizontal"></asp:RadioButtonList> 2 <br /> 3 <asp:Button ID="Button1" runat="server" Text="Select" />
在aspx.vb中,为RadioButtonList绑定数据源,当然绑定数据源下面的代码中,还得引用命名空间 Imports Insus.NET

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load If Not IsPostBack Then Data_Binding() End If End Sub Private Sub Data_Binding() Dim objCosmetic As New Cosmetic() Me.RadioButtonListCosmetic.DataSource = objCosmetic.GetData() Me.RadioButtonListCosmetic.DataTextField = "Name" Me.RadioButtonListCosmetic.DataValueField = "ID" Me.RadioButtonListCosmetic.DataBind() End Sub
接下来是演示开始,写Javascript代码:

<script type="text/javascript"> function CheckIsSelected() { var rbl = document.getElementById("<%=RadioButtonListCosmetic.ClientID%>"); var radio = rbl.getElementsByTagName("input"); var isSelect = false; for (var i = 0; i < radio.length; i++) { if (radio[i].checked) { isSelect = true; break; } } if (!isSelect) { alert("请选择一个选项。"); } return isSelect; } </script>
最后是为铵钮Button写客户端事件
<asp:Button ID="Button1" runat="server" Text="Select" OnClientClick="return CheckIsSelected()" />
标签:
JavaScript
, VB.net
, js
, RadioButtonList
, List
, document
, getElementById
, OnClientClick
, getElementsByTagName
, ClientID
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2012-01-14 MS SQL动态创建临时表
2011-01-14 SQL怎么取指定周的最后一天
2011-01-14 查找今天内,时间最大的一条记录